2013年1月15日 星期二

javascript 算個時間的差距

1. 先轉成timestamp

http://stackoverflow.com/questions/9873197/convert-date-to-timestamp-in-javascript
http://stackoverflow.com/questions/1968167/difference-between-dates-in-javascript

ex.

var confirm_date = "2012-01-15 12:34:56";
confirm_date = confirm_date.split(" ");
var [year,month,day] = confirm_date[0].split("-");
var confirm_timestamp = new Date(year,month,day).getTime();

2. 再相減 timestamp後除以你要比的時間單位(天,小時,分,秒)
http://stackoverflow.com/questions/1787939/check-time-difference-in-javascript
http://blogs.digitss.com/javascript/calculate-datetime-difference-simple-javascript-code-snippet/
ex.
var days_difference = Math.floor((now_timestamp - confirm_timestamp)/1000/60/60/24);








沒有留言:

張貼留言