如何使用javascript将时间(上午12:30)转换为时间戳? [英] How can i convert a time (12:30 am) into timestamp using javascript?

查看:266
本文介绍了如何使用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)转换为时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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