如何使用javascript将时间(上午12:30)转换为时间戳?

编程入门 行业动态 更新时间:2024-10-25 00:25:29
本文介绍了如何使用javascript将时间(上午12:30)转换为时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

谁能告诉我怎么做?我想比较2次,找出哪一个更大?喜欢下午12:30和下午5:30

Can anyone tell me how to that? I would like to compare 2 times and find out which one is greater? like 12:30 pm and 5:30 pm

推荐答案

将时间变成 javascript日期,在日期上调用 getTime()以返回自1970年1月1日午夜以来的毫秒数。

Turn the times into javascript dates, call getTime() on the dates to return the number of milliseconds since midnight Jan 1, 1970.

比较每个日期的 getTime()返回值,以确定哪个更大。

Compare the getTime() returned values on each date to determine which is greater.

对于12小时格式,下面的代码可以使用。

For 12 Hour format the code below will work.

var date1 = new Date('Sat Sep 24 2011 12:30:00 PM').getTime(); //12:30 pm var date2 = new Date('Sat Sep 24 2011 5:30:00 PM').getTime(); //5:30 pm if(date1 > date2) { alert('date1 is greater'); } else if(date2 > date1) { alert('date2 is greater'); } else { alert('dates are equal'); }

更多推荐

如何使用javascript将时间(上午12:30)转换为时间戳?

本文发布于:2023-10-25 14:17:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527201.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   转换为   如何使用   上午   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!