javascript专有日期格式和原型

时间:2012-03-10 22:06:27

标签: javascript prototype

我使用的专有日期格式如下:

var TheUserDate = "3.11.2012.4.3"; // is March 11th 2012 4:03AM

我创建了这种格式,因为我的应用程序使用了很多时区更改,而且我不想依赖浏览器的时钟。

我到处都有这样的代码:

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);

如何重写它以便模拟javascript中内置的.getMonth()。getYear()函数。我想我需要修改字符串的原型,但这是我第一次做这样的事情。我希望有这样的功能:

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.

我该怎么做?

感谢。

2 个答案:

答案 0 :(得分:2)

使用ISO8601日期格式YYYY-MM-DD THH:mm:ssZ并且有javascript的日期时间标准化库以及UTC方法以避免时区问题。 JavaScript中Date值的最大问题是Date对象中存在大量未定义的行为。对于需要一致的Date api的人,我建议使用一个覆盖默认Date对象的固定实现,允许在所有浏览器中保持一致的行为,但是如果你有其他依赖于它的库,那就要小心了。

我过去曾使用https://github.com/csnover/js-iso8601/而没有问题

http://www.w3.org/TR/NOTE-datetime

Does Javascript/EcmaScript3 support ISO8601 date parsing?

更新

根据要求,如果您不想使用符合标准的日期格式,要实际达到您想要的效果,这就是您实现所需对象的方法

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;

}());

在控制台中进行测试:

> 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

严重的是不要考虑编辑String.prototype来为您提供此功能。除非你真的确定自己在做什么,否则在JavaScript中扩展原生原型是非常糟糕的做法。将这种定制功能添加到通用字符串对象当然没有任何意义。如果您需要对此UserDate对象进行全局访问,请将其设置为本示例中的全局对象。危险在于您不知道第三方库正在对原型做什么。

http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/

如果你对一般的论点感兴趣,可能值得一读。如果你真的真的想要扩展字符串原型,那么你可以做类似

的事情
String.prototype.getDay = function () {
    return this.split('.')[0];
}

但这会让我感到伤心。

答案 1 :(得分:0)

日期非常简单。

var d = Date.now()
var dateString = new Date(d).toGMTString()

将日期存储为UTC时间戳,如果您想要以可视方式呈现它,请致电new Date(timestamp).toGMTString()

相关问题