如何使用更具体的javascript获取差异日期

时间:2019-01-31 04:57:11

标签: javascript date-format date-difference

大家好,我想使用javascript获取差异日期,在php im中使用此脚本获取差异日期:

 $tgl1 = date('Y-m-d');  
 $tgl2 = '2019-12-31';

 $pecah1 = explode("-", $tgl1);
 $date1 = $pecah1[2];
 $month1 = $pecah1[1];
 $year1 = $pecah1[0];
 $pecah2 = explode("-", $tgl2);
 $date2 = $pecah2[2];
 $month2 = $pecah2[1];
 $year2 =  $pecah2[0];

 $jd1 = GregorianToJD($month1, $date1, $year1);
 $jd2 = GregorianToJD($month2, $date2, $year2);
 $selisih = $jd2 - $jd1;

以及如何使用javascript

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助:)

date1 = "2018-12-25"; //date1
date2 = "2019-01-01"; //date2

var one_day = 1000 * 60 * 60 * 24;

var x = date1.split("-");
var y = date2.split("-");

var new_date1 = new Date(x[0], (x[1] - 1), x[2]);
var new_date2 = new Date(y[0], (y[1] - 1), y[2]);

var month1 = x[1] - 1;
var month2 = y[1] - 1;

_Diff = Math.ceil((new_date2.getTime() - new_date1.getTime()) / (one_day));

console.log(_Diff);