Any way to parse a time string using Moment.js but ignore timezone info?

前端 未结 10 1563
深忆病人
深忆病人 2020-12-30 01:56

Given the volume of Timezone questions, I would have thought to be able to find the answer to this issue, but haven\'t had any success.

Is there a way using mo

相关标签:
10条回答
  • 2020-12-30 02:15

    You can ignore the browser's timezone completely by creating a new moment using moment.utc() instead of moment().

    For example, if you are trying to work purely with a UTC date/time of the browser's current time but want to discard its timezone data, you can recreate the browser's current time into a UTC format using the following:

    let nowWithTimezone = moment();
    let nowInUtc = moment.utc(nowWithTimezone.format('MM/DD/YYYY HH:mm'), 'MM/DD/YYYY HH:mm');
    

    Further documentation on moment.utc(): https://momentjs.com/docs/#/parsing/utc/

    0 讨论(0)
  • 2020-12-30 02:16

    Use moment.parseZone to convert without taking into account the timezone.

    const moment = require('moment')
    
    const dateStr = '2020-07-21T10:00:00-09'
    
    const date = moment.parseZone(dateStr)
    
    console.log(date.format('MM-DD-YY HH:mm A')) // 07-21-20 10:00 AM
    

    Try here link to docs

    0 讨论(0)
  • 2020-12-30 02:17

    This is difficult task to do with MomentJS, it will basically depend as well on your current timezone.

    Documentation as well is vague for this specific task, the way I solved the issue on my side was by adding hours to the date before converting it to JSON format.

    var dt = moment("Sun Sep 13 2015 00:00:00 GMT-0400", "ffffd MMM DD YYYY HH:mm:ss GMT-0400", false);
    var date = dt.add(2, 'hour').toJSON();
    console.log(date); //2015-09-13T00:00:00.000Z
    
    0 讨论(0)
  • 2020-12-30 02:18

    There are valid reasons to do what the OP is asking for. The easiest way to do this with Moment is using its parseZone(date) method. No futzing around with string manipulation or multiple calls. It effectively parses the date string as though it were in the browser's local time zone.

    0 讨论(0)
  • 2020-12-30 02:24

    If you know for sure your input string is in the ISO-8601 format, you could just strip off the last 5 digits and use that in the Moment constructor.

    var input = "2012-12-31T00:00:00+0000"
    input = input.substring(0, input.length-5)
    moment(input).toString()
    > "Mon Dec 31 2012 00:00:00 GMT-0600"
    
    0 讨论(0)
  • 2020-12-30 02:27

    I don't think you really want to ignore the offset. That would ultimately just be replacing the offset you provided with one from your local time zone - and that would result in a completely different moment in time.

    Perhaps you are just looking for a way to have a moment retain the time zone it was given? If so, then use the moment.parseZone function. For example:

    var m = moment.parseZone("2012-12-31T00:00:00+0000");
    var s = m.format();   // "2012-12-31T00:00:00+00:00"
    

    You could also achieve this with moment.utc. The difference is that moment.parseZone will retain whatever offset you give it, while moment.utc will adjust to UTC if you give it a non-zero offset.

    0 讨论(0)
提交回复
热议问题