-5

hello guys i would like to get difference date using javascript, in php Im using this script to get difference date :

 $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;

and how to do in javascript

doğukan
  • 14,001
  • 6
  • 27
  • 47

1 Answers1

0

This may help :)

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);
p u
  • 1,338
  • 1
  • 11
  • 26