Reputation: 51997
I use a proprietary date format that looks like this:
var TheUserDate = "3.11.2012.4.3"; // is March 11th 2012 4:03AM
I created this format because my application uses a lot of timezone changes and I didn't want to rely on the browser's clock for that.
I have code like this everywhere:
var DateArray = TheUserDate.split(".");
var TheMonth = parseInt($.trim(DateArray[0]), 10);
var TheDay = parseInt($.trim(DateArray[1]), 10);
var TheYear = parseInt($.trim(DateArray[2]), 10);
How can I rewrite this so that it emulated the .getMonth() .getYear() functions that are built into javascript. I'm thinking I need to modify the prototype of strings but this is my first attempt at doing something like this. I'd like to have functions like:
var TheMonth = TheUserDate.getMyMonth();
var TheDay = TheUserDate.getMyDay();
var TheYear = TheUserDate.getMyYear();
var TheDate = TheUserDate.getMyDate(); // to convert my format back to a javascript date.
How should I do this?
Thanks.
Upvotes: 0
Views: 769
Reputation: 22966
Use the ISO8601 date format YYYY-MM-DD THH:mm:ssZ and there are datetime standardisation libraries for javascript, along with the UTC methods to avoid timezone issues. The biggest issue with Date values in JavaScript is that there is a lot of undefined behaviour in the Date objects. For anyone who needs a consistent Date api I would suggest using a fixed implementation that overrides the default Date object, allowing for consistent behaviour across all browsers, but do be careful if you have other libraries which are dependent on this.
I have used https://github.com/csnover/js-iso8601/ in the past without issues
http://www.w3.org/TR/NOTE-datetime
Does Javascript/EcmaScript3 support ISO8601 date parsing?
As requested, to actually achieve what you want if you don't want to use a standards compliant date format, this is how you might go about implementing the object you want
var UserDate = (function () {
function UserDate(dateString) {
var dateArray= dateString.split('.'), i;
if (dateArray.length !== 5) {
// Handle this however you want, throw exception,
// bad date type etc
}
// No need to trim, parseInt doesn't care about
// leading or trailing whitespace
for (i = 0; i < dateArray.length; i += 1) {
dateArray[i] = parseInt(dateArray[i], 10);
}
// Check that the date array is well formed here if you want,
// check for NaN and value range
// ...
this._dateArray = dateArray;
}
// Creates a date string from the internal date array
UserDate.prototype.getDate = function () {
var dateString = "", i;
for (i = 0; i < this._dateArray.length; i += 1) {
dateString += this._dateArray[i];
if (i < this._dateArray.length - 1) {
dateString += ".";
}
}
return dateString;
};
// Returns the day value from the internal date array
UserDate.prototype.getDay = function () {
return this._dateArray[0];
};
// Returns the month from the internal date array
UserDate.prototype.getMonth = function () {
return this._dateArray[1];
};
// Returns the year from the internal data array
UserDate.prototype.getYear = function() {
return this._dateArray[2];
};
// Returns the hour from the internal date array
UserDate.prototype.getHour = function() {
return this._dateArray[3];
};
// Returns the minute from the internal date array
UserDate.prototype.getMinute = function() {
return this._dateArray[4];
};
// more prototypes here
return UserDate;
}());
With my tests in the console:
> var someDate = new UserDate("3.11.2012.4.3");
> someDate.getDate()
"3.11.2012.4.3"
> someDate.getYear()
2012
> someDate.getMonth()
11
> someDate.getDay()
3
> someDate.getHour()
4
> someDate.getMinut()
3
Seriously don't consider editing String.prototype to give you this functionality. It's very bad practice to extend the native prototypes in JavaScript unless you are really sure what you are doing. It certainly doesn't make any sense to be adding such bespoke functionality to a general purpose string object. If you need global access to this UserDate object then make it a global object as it would be in this example. The danger is that you don't know what third party libraries are doing to the prototype.
http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/
Might be worth a read if you're interested in the arguments in general. If you really really really want to extend the string prototype then you could do something like
String.prototype.getDay = function () {
return this.split('.')[0];
}
But this will make me a sad chicken.
Upvotes: 2
Reputation: 169471
Dates are really simple.
var d = Date.now()
var dateString = new Date(d).toGMTString()
Store dates as the UTC timestamp and call new Date(timestamp).toGMTString()
if you ever want to visually render it.
Upvotes: 0