本文介绍了如何使用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)转换为时间戳?
发布评论