How to parse local time without a date with moment?

雨燕双飞 提交于 2021-02-11 13:57:13

问题


Input 8:30 PM with the local date of May 12th 2020 while UTC date already May 13th 2020

Desired Output 2020-05-12 20:30:00

Actual Output 2020-05-13 20:30:00


Tried 3 things:

result = moment('8:30 PM', 'h:mm A').format('YYYY-MM-DD HH:mm:ss');

result = moment.tz('8:30 PM', 'h:mm A', 'America/New_York').format('YYYY-MM-DD HH:mm:ss');

moment.tz.setDefault('America/New_York');
result = moment.tz('8:30 PM', 'h:mm A', 'America/New_York').format('YYYY-MM-DD HH:mm:ss');

回答1:


As user120242 says it seems that moment.tz uses the current UTC date when creating a date from a time. If you want to use the current date in the target timezone, a kludge is to create a string for the date, tack on your time string then parse that, e.g.

moment.tz.setDefault('America/New_York');

let timeString    = '8:30 PM';
let inputFormat   = 'YYYY-MM-DD HH:mm A'
let dateFormat    = 'YYYY-MM-DD';
let displayFormat = 'YYYY-MM-DD HH:mm:ss ZZ';

// Current date in New York
let now = moment();
// Create a timestamp for required time
let nyString = now.format(dateFormat) + ' ' + timeString;
// Create a date for 8:30 in New York
let then = moment(nyString, inputFormat);

console.log('Now in New York    : ' + now.format(displayFormat));
console.log(timeString + ' in New York: ' + then.format(displayFormat));
console.log('Sans kludge in NY  : ' + moment(timeString, 'HH:mm A').format(displayFormat));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment-with-locales.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.28/moment-timezone-with-data.js"></script>

To see it work you need to set your clock so New York is a different date to UTC.




回答2:


When running the second piece of code tried there, the console contains an error.

Moment Timezone has no data for America/New_York. See http://momentjs.com/timezone/docs/#/data-loading/.

If you haven't yet loaded a set of time zones into moment, it wont have data to use for the parsing. Check that you've loaded the time zone data into moment before trying to run any time zone related functions. If you are unsure of how to load the data into moment, check the part of the docs listed in the error message, here.




回答3:


Please clarify what problem you are actually solving;
OP, you shouldn't do this anyways, because the time becomes ambiguous if it is sent to another computer. If the time is currently 12AM or 11:59PM, with people in different time zones, the time you send becomes ambiguous.
If you really still must do it this way, you will have to set the date on the result, or set hours and minutes manually after calculating the "desired" date, to always calculate it to be a time within the last 24 hours.

Yes, there is an issue with the timezone plugin. In actuality moment() also misbehaves when it's a time before now. But I can't think of a valid use case for using it the way you are trying to use it, and how it should behave isn't well defined anymore with how you're using it.

Proof of the issue:

moment.tz.setDefault('America/New_York');
result = moment.tz('8:30 PM', 'h:mm A', 'America/New_York').format('YYYY-MM-DD HH:mm:ss');

// set your clock to a time when UTC date will be tomorrow (or yesterday) to see the problem
console.log([result,
moment('12:01AM', 'h:mm A').format('YYYY-MM-DD HH:mm:ss'),
moment.tz('12:01 AM', 'h:mm A', moment.tz.guess()).format('YYYY-MM-DD HH:mm:ss'),
moment('11:59PM', 'h:mm A').format('YYYY-MM-DD HH:mm:ss'),
moment.tz('11:59 PM', 'h:mm A', moment.tz.guess()).format('YYYY-MM-DD HH:mm:ss')].join('\n')
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.25.3/moment-with-locales.min.js" integrity="sha256-8d6kI5cQEwofkZmaPTRbKgyD70GN5mDpTYNP9YWhTlI=" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.28/moment-timezone-with-data.js" integrity="sha256-O1PdKrSbpAYWSBteb7yX/CMmHhu3US31mtCbsryGwaY=" crossorigin="anonymous"></script>


来源:https://stackoverflow.com/questions/61764708/how-to-parse-local-time-without-a-date-with-moment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!