creating a UtcDate object in javascript
I am certain this has been beaten to death, but I can't find it. I want to create a simple UtcDate object in javascript. I actually have one that works ...
(function () {
UtcDate = (function () {
var date;
if (arguments.length > 0) {
date = new Date(arguments[0], arguments[1], arguments[2]);
}
else if (arguments.length == 0) {
date = new Da开发者_JAVA百科te();
}
return new Date(date.toUTCString());
});
})();
I would like to make it a bit more intelligent, though. Is there any way to do this without needing the explicit argument indexers? The basic idea is that you can pass in parameters just like a normal date, and if not, it just creates the current date.
It can be optimised like that. If arguments.length is 0, means that it is false.
About the argument indexers - yes, they can be changed to arguments (with default value). If no, or some of arguments are passed the missing will be replaced with default. If none is passed the date will be set to 12 april 2000.
UtcDate = (function (year,month,day) {
var date;
if (!arguments.length) {
date = new Date();
} else {
date = new Date(year||2000,month||3,day||12);
}
return new Date(date.toUTCString());
});
I`m not sure why you need the toUTCString(). FireFox, Chrome, Opera return date in the UTC format.
精彩评论