[英]How to convert Date to timestamp using MomentJS?
I used MomentJS to convert local date to UTC date using the following way:我使用 MomentJS 使用以下方式将本地日期转换为 UTC 日期:
$("#div1").text(moment("2016-10-11 18:06:03").tz("Europe/Paris").format());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.6/moment-timezone-with-data.min.js"></script> <div id="div1"></div>
Now I need timestamp from the output value using MomentJS.现在我需要使用 MomentJS 输出值的时间戳。
moment().format("X"); // lowercase 'x' for milliseconds
var date = moment('2016-10-11 18:06:03').tz('Europe/Paris').format(), timestamp = moment(date).format("X"); $('#div1').text(date); $('#timestamp').text(timestamp);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.min.js"></script> <div id="div1"></div> <div id="timestamp"></div>
You said:你说:
I used MomentJS to convert local date to UTC date using the following way:
moment("2016-10-11 18:06:03").tz("Europe/Paris").format()
我使用 MomentJS 使用以下方式将本地日期转换为 UTC 日期:moment("2016-10-11 18:06:03").tz("Europe/Paris").format()
That doesn't do that.那不这样做。 That converts a local value to Paris time, and emits it as a string in ISO8601 format.这会将本地值转换为巴黎时间,并将其作为 ISO8601 格式的字符串发出。
Now I need timestamp from the output value using MomentJS.现在我需要使用 MomentJS 输出值的时间戳。
That's a different question, and wouldn't involve the output of the above because:这是一个不同的问题,不会涉及上述输出,因为:
You can't get a timestamp from the output string, you'd get it from a moment
object.您无法从输出字符串中获取时间戳,而只能从moment
对象中获取。 You could parse that string, but that would be silly since you already had a moment
object earlier.您可以解析该字符串,但这很愚蠢,因为您之前已经有了一个moment
对象。
Timestamps are UTC based, so time zone conversion is irrelevant.时间戳基于 UTC,因此时区转换无关紧要。 You'd get the same timestamp if you didn't convert at all.如果您根本不转换,您将获得相同的时间戳。
You can get a string with a timestamp using .format('X')
or .format('x')
depending on which precision you want.您可以使用.format('X')
或.format('x')
获取带有时间戳的字符串,具体取决于所需的精度。 But it's much cleaner to just get the numerical timestamp using .valueOf()
or .unix()
, again depending on precision.但是使用.valueOf()
或.unix()
获取数字时间戳要干净得多,同样取决于精度。
To get the time from the date use the format method of momentjs要从日期中获取时间,请使用 momentjs 的 format 方法
var date = moment('2016-10-11 18:06:03').tz('Europe/Paris').format(); console.log(date); console.log(moment(date).format("X")); console.log(moment(date).format("x")); // for milliseconds console.log(moment(date).format("HH:mm")); console.log(moment(date).format("hh:mm A"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data.min.js"></script>
Just to expand on something mentioned earlier.只是为了扩展前面提到的东西。 These generate the results.这些会产生结果。
// less precision
moment().unix() === moment().format('X') // uppercase X
// more precision
moment().valueOf() === moment().format('x') // lowercase x
Hope that sheds some light希望能有所启发
I think moment().unix()
do the trick !我认为moment().unix()
可以解决问题! See https://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/见https://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.