首页 文章

没有时区javascript的解析日期

提问于
浏览
101

我想在JavaScript中解析没有时区的日期 . 我试过了:

new Date(Date.parse("2005-07-08T00:00:00+0000"));

返回Fri Jul 08 2005 02:00:00 GMT+0200 (中欧夏令时)

new Date(Date.parse("2005-07-08 00:00:00 GMT+0000"));

返回相同的结果

new Date(Date.parse("2005-07-08 00:00:00 GMT-0000"));

返回相同的结果

我想解析一下时间:

  • 没有时区 .

  • 不调用构造函数Date.UTC或新日期(年,月,日) .

  • 只需简单地将字符串传递给Date构造函数(没有原型方法) .

  • 我要产品 Date 对象,而不是 String .

9 回答

  • 10

    日期被正确解析,只是toString将其转换为您当地的时区:

    > new Date(Date.parse("2005-07-08T11:22:33+0000"))
    Fri Jul 08 2005 13:22:33 GMT+0200 (CEST)
    > new Date(Date.parse("2005-07-08T11:22:33+0000")).toUTCString()
    "Fri, 08 Jul 2005 11:22:33 GMT"
    

    Javascript Date对象是时间戳 - 它们仅包含自纪元以来的毫秒数 . Date对象中没有时区信息 . 此时间戳表示的日历日期(日,分,秒)是解释的问题( to...String 方法之一) .

    上面的示例显示正确解析日期 - 也就是说,它实际上包含与GMT中的“2005-07-08T11:22:33”对应的毫秒数 .

  • -1

    我有同样的问题 . 我得到一个String作为日期,例如:'2016-08-25T00:00:00',但我需要让Date对象有正确的时间 . 要将String转换为对象,我使用getTimezoneOffset:

    var date = new Date('2016-08-25T00:00:00')
    var userTimezoneOffset = date.getTimezoneOffset() * 60000;
    new Date(date.getTime() - userTimezoneOffset);
    

    getTimezoneOffset() 将返回以太网负值或正值 . 必须减去它才能在世界上的每个位置工作 .

  • 5

    我遇到了同样的问题,然后想起了我正在研究的遗留项目以及他们如何处理这个问题 . 我当时并不理解,直到我自己遇到问题才真正关心

    var date = '2014-01-02T00:00:00.000Z'
    date = date.substring(0,10).split('-')
    date = date[1] + '-' + date[2] + '-' + date[0]
    
    new Date(date) #Thu Jan 02 2014 00:00:00 GMT-0600
    

    无论出于何种原因将日期传递为'01 -02-2014',将时区设置为零并忽略用户的时区 . 这可能是Date类中的一个侥幸,但它存在于前一段时间并且存在于今天 . 它似乎跨浏览器工作 . 亲自试试吧 .

    此代码在一个全局项目中实现,其中时区很重要,但查看日期的人并不关心它引入的确切时刻 .

  • 0

    无论如何, Date 对象本身都将包含时区,返回的结果是以默认方式将其转换为字符串的效果 . 即如果没有时区,则无法创建日期对象 . 但你可以做的是通过创建自己的对象来模仿 Date 对象的行为 . 但是,最好将其移交给像moment.js这样的图书馆 .

  • -6

    简单解决方案

    const handler1 = {
      construct(target, args) {
        let newDate = new target(...args);
        var tzDifference = newDate.getTimezoneOffset();
        return new target(newDate.getTime() + tzDifference * 60 * 1000);
      }
    };
    
    Date = new Proxy(Date, handler1);
    
  • 2

    只是一个通用的说明 . 一种保持灵活性的方法 .

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

    我们可以使用getMinutes(),但它在前9分钟只返回一个数字 .

    let epoch = new Date() // Or any unix timestamp
    
    let za = new Date(epoch),
        zaR = za.getUTCFullYear(),
        zaMth = za.getUTCMonth(),
        zaDs = za.getUTCDate(),
        zaTm = za.toTimeString().substr(0,5);
    
    console.log(zaR +"-" + zaMth + "-" + zaDs, zaTm)
    
    Date.prototype.getDate()
        Returns the day of the month (1-31) for the specified date according to local time.
    Date.prototype.getDay()
        Returns the day of the week (0-6) for the specified date according to local time.
    Date.prototype.getFullYear()
        Returns the year (4 digits for 4-digit years) of the specified date according to local time.
    Date.prototype.getHours()
        Returns the hour (0-23) in the specified date according to local time.
    Date.prototype.getMilliseconds()
        Returns the milliseconds (0-999) in the specified date according to local time.
    Date.prototype.getMinutes()
        Returns the minutes (0-59) in the specified date according to local time.
    Date.prototype.getMonth()
        Returns the month (0-11) in the specified date according to local time.
    Date.prototype.getSeconds()
        Returns the seconds (0-59) in the specified date according to local time.
    Date.prototype.getTime()
        Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative for prior times).
    Date.prototype.getTimezoneOffset()
        Returns the time-zone offset in minutes for the current locale.
    Date.prototype.getUTCDate()
        Returns the day (date) of the month (1-31) in the specified date according to universal time.
    Date.prototype.getUTCDay()
        Returns the day of the week (0-6) in the specified date according to universal time.
    Date.prototype.getUTCFullYear()
        Returns the year (4 digits for 4-digit years) in the specified date according to universal time.
    Date.prototype.getUTCHours()
        Returns the hours (0-23) in the specified date according to universal time.
    Date.prototype.getUTCMilliseconds()
        Returns the milliseconds (0-999) in the specified date according to universal time.
    Date.prototype.getUTCMinutes()
        Returns the minutes (0-59) in the specified date according to universal time.
    Date.prototype.getUTCMonth()
        Returns the month (0-11) in the specified date according to universal time.
    Date.prototype.getUTCSeconds()
        Returns the seconds (0-59) in the specified date according to universal time.
    Date.prototype.getYear()
        Returns the year (usually 2-3 digits) in the specified date according to local time. Use getFullYear() instead.
    
  • -1

    (new Date().toString()).replace(/ \w+-\d+ \(.*\)$/,"")

    这将有输出:2018年7月10日星期二19:07:11

    (new Date("2005-07-08T11:22:33+0000").toString()).replace(/ \w+-\d+ \(.*\)$/,"")

    这将有输出:Fri Jul 08 2005 04:22:33

    注意:返回的时间取决于您当地的时区

  • 76

    这是我想出的解决这个问题的解决方案 .


    使用的库:具有普通javascript日期类的momentjs .

    步骤1.将字符串日期转换为时刻对象(PS:时刻保留原始日期和时间,只要未调用 toDate() 方法):

    const dateMoment = moment("2005-07-08T11:22:33+0000");

    步骤2.从先前创建的时刻对象中提取 hoursminutes 值:

    const hours = dateMoment.hours();
      const mins = dateMoment.minutes();
    

    步骤3.将时刻转换为日期(PS:这将根据您的浏览器/机器的时区更改原始日期,但不要担心并阅读第4步 . ):

    const dateObj = dateMoment.toDate();
    

    步骤4.手动设置步骤2中提取的小时和分钟 .

    dateObj.setHours(hours);
      dateObj.setMinutes(mins);
    

    步骤5. dateObj 现在将显示原始日期,没有任何时区差异 . 即使日光时间更改也不会对日期对象产生任何影响,因为我们手动设置原始小时和分钟 .

    希望这可以帮助 .

  • 75

    日期解析存在一些固有的问题,遗憾的是,这些问题没有得到很好的解决 .

    • 人类可读日期中隐含着时区
    • 网络上有许多广泛使用的日期格式含糊不清

    为了解决这些容易和干净的问题,人们需要这样的功能:

    >parse(whateverDateTimeString,expectedDatePattern,timezone)
    "unix time in milliseconds"
    

    我已经搜索过这个,但没有发现这样的东西!

    所以我创建了:https://github.com/zsoltszabo/timestamp-grabber

    请享用!

相关问题