如何在没有时刻的日期的情况下解析当地时间? [英] How to parse local time without a date with moment?

查看:55
本文介绍了如何在没有时刻的日期的情况下解析当地时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入 8:30 PM 本地日期为 2020 年 5 月 12 日,UTC 日期已经是 2020 年 5 月 13 日

期望输出 2020-05-12 20:30:00

实际输出2020-05-13 20:30:00

<小时>

尝试了 3 件事:

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

解决方案

正如 user120242 所说,在从某个时间创建日期时,moment.tz 似乎使用当前的 UTC 日期.如果你想使用目标时区中的当前日期,一个kludge是为日期创建一个字符串,添加你的时间字符串然后解析它,例如

moment.tz.setDefault('America/New_York');让 timeString = '8:30 PM';让 inputFormat = 'YYYY-MM-DD HH:mm A'让 dateFormat = 'YYYY-MM-DD';let displayFormat = 'YYYY-MM-DD HH:mm:ss ZZ';//纽约的当前日期让现在=时刻();//创建所需时间的时间戳让 nyString = now.format(dateFormat) + ' ' + timeString;//在纽约创建一个 8:30 的日期let then = moment(nyString, inputFormat);console.log('现在在纽约:' + now.format(displayFormat));console.log(timeString + ' 在纽约:' + 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>

要使其正常工作,您需要设置时钟,使纽约与 UTC 日期不同.

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

解决方案

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.

这篇关于如何在没有时刻的日期的情况下解析当地时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆