What is the appropriate function for getting the days (numeric) difference between two xs.date type?
var date1 = new Date();
var date2 = new Date();
date1 = xs.date("2018-03-29");
date2 = xs.date("2018-04-15");
I tried using fn.daysFromDuration function (see sample below), but it is not returning a numeric value which represents the days difference. It is returning a xs.dayTimeDuration:
var dateDiff = new Date();
dateDiff = fn.daysFromDuration(date2 - date1);
You can't use date2 - date1 to calculate the difference between dates and yield a xs.dayTimeDuration in JavaScript, like you can in XQuery.
In an SJS module, use the xs.date.subtract() from the xs.date object, then you can use fn.daysFromDuration() to obtain the number of days as a number:
const date1 = xs.date("2018-03-29");
const date2 = xs.date("2018-04-15");
fn.daysFromDuration(date2.subtract(date1));
Related
This question already has answers here:
compare string with today's date in JavaScript
(8 answers)
Closed 4 years ago.
I am trying to compare dates with different formats. I see wrong results when I compare the below, How can I converts dates to a standard format to get correct results.
Heres my Fiddle
var date1 = "4/12/2018 9:52:21 PM";
var date2 ="4/12/2018 9:52:51 PM";
var date3 ="2018/04/12 21:54:40";
var dateCondition1 = (date3>date2);
var dateCondition2 = (date2>date1);
alert(dateCondition1); //shows wrong result
alert(dateCondition2); //shows right result
will this be a correct comparison?
var date1 = new Date("4/12/2018 9:52:21 PM");
var date2 =new Date("4/12/2018 9:52:51 PM");
var date3 =new Date("2018/04/12 21:54:40");
var dateCondition1 = (date3>date2);
var dateCondition2 = (date2>date1);
alert(dateCondition1); //shows wrong result
alert(dateCondition2); //shows right result
try with pure javascript:
var date1 = new Date("4/12/2018 9:52:21 PM");
var date2 =new Date("4/12/2018 9:52:51 PM");
var date3 =new Date("2018/04/12 21:54:40");
var dateCondition1 = (date3.getTime() > date2.getTime());
var dateCondition2 = (date2.getTime() > date1.getTime());
alert(dateCondition1);
alert(dateCondition2);
in the first block of code you are comparing strings
"4/12/2018 9:52:21 PM" > "4/12/2018 9:52:21 PM";
in the second block of code the comparison is correct,
Why does a boolean return in the comparison of 2 strings?
"Matt Ball"
Because, as in many programming languages, strings are compared lexicographically.
You can think of this as a fancier version of alphabetical ordering, the difference being that alphabetic ordering only covers the 26 characters a through z.
ALTERNATIVE
using the library moment js is easier
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
//Currrent Date
var now = moment(),
custom = moment('Mon 03-Jul-2017, 11:00 AM', 'ddd DD-MMM-YYYY, hh:mm A');
document.write("Compare dates=>" + now.isAfter(custom));
</script>
if you want to use the three comparisons you can format the dates as you want
<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
<script>
var now = moment();
var date1 = moment('4/12/2018 9:52:21 PM','DD/MM/YYYY hh:mm:ss A');
var date2 = moment('4/12/2018 9:52:51 PM','DD/MM/YYYY hh:mm:ss A');
var date3 = moment('2018/04/12 21:54:40','YYYY/DD/MM hh:mm:ss A');
var dateCondition1 = (date3.isAfter(date2));
var dateCondition2 = (date2.isBefore(date1));
alert(dateCondition1);
alert(dateCondition2);
</script>
</html>
I have the current script. var date 2 needs to automatically grab the current date and then do the calculation. I have tried many things with no luck, please help.
<script>
// Here are the two dates to compare
var date1 = '2015-09-08';
var date2 = '2015-12-13';
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[0], date1[1], date1[2]);
date2 = new Date(date2[0], date2[1], date2[2]);
// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000)
date1_unixtime = parseInt(date1.getTime() / 1000);
date2_unixtime = parseInt(date2.getTime() / 1000);
// This is the calculated difference in seconds
var timeDifference = date2_unixtime - date1_unixtime;
// in Hours
var timeDifferenceInHours = timeDifference / 60 / 60;
// in weeks :)
var timeDifferenceInWeeks = timeDifferenceInHours / 24/7;
document.write(Math.ceil(timeDifferenceInWeeks));
</script>
You can use new Date() to get current date. please refer snippet.
<script>
// Here are the two dates to compare
var date1 = '2015-09-08';
var date2 = '2015-12-13';
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day
date1 = date1.split('-');
date2 = date2.split('-');
// Now we convert the array to a Date object, which has several helpful methods
date1 = new Date(date1[0], date1[1]-1, date1[2]);
date2 = new Date(date2[0], date2[1]-1, date2[2]);
// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000)
date1_unixtime = parseInt(date1.getTime());
date2_unixtime = parseInt(date2.getTime());
date3_unixtime = parseInt((new Date()).getTime());
// This is the calculated difference in seconds
var timeDifference = date2_unixtime - date1_unixtime;
var timeDifferenceInWeeks1 = timeDifference / (1000*60*60*24*7);
var timeDifferenceInWeeks2 = (date3_unixtime - date2_unixtime) / (1000*60*60*24*7);
document.write(Math.ceil(timeDifferenceInWeeks1)+'--'+Math.ceil(timeDifferenceInWeeks2));
</script>
just use
var date2 = new Date();
now the date2 holds the current date and time from your machine.
I changed to a php code and the following is what I got to work.
<?
$strtDate = '2015-09-08';
$endDate = date('Y-m-d');
//$endDate = 'new Date();';
$startDateWeekCnt = round(floor( date('d',strtotime($strtDate)) / 7)) ;
// echo $startDateWeekCnt ."\n";
$endDateWeekCnt = round(ceil( date('d',strtotime($endDate)) / 7)) ;
//echo $endDateWeekCnt. "\n";
$datediff = strtotime(date('Y-m',strtotime($endDate))."-01") - strtotime(date('Y-m',strtotime($strtDate))."-01");
$totalnoOfWeek = round(floor($datediff/(60*60*24)) / 7) + $endDateWeekCnt - $startDateWeekCnt ;
echo $totalnoOfWeek ."\n";
?>
I have some dates in some different format.
date1 = 2015-05-27T04:51:12.715Z
date2 = 2015-05-27T04:51:12.782Z
date3 = 2015-05-27T04:51:12.865Z
Dont know what this format means specially the last 4 characters.And how would I find the maximum of these dates in Javascript
Just sort the array and get the first item
var date1 = '2015-05-27T04:51:12.715Z';
var date2 = '2015-05-27T04:51:12.782Z';
var date3 = '2015-05-27T04:51:12.865Z';
var array = [date1, date2, date3];
array.sort(function(a,b) {
return new Date(a) < new Date(b);
});
var max = array[0];
I want to calculate the difference between two dateTime, one date is submitted by user and other is current time:
user submitted time - now = difference in unix
user submitted time format is:
2014-03-26 10:52:00
Thanks for your help.
You can simply do this with getTime() which returns the number of milliseconds.
var ds = "2014-03-26 10:52:00";
var newDate = new Date(ds).getTime(); //convert string date to Date object
var currentDate = new Date().getTime();
var diff = currentDate-newDate;
console.log(diff);
Sometimes there are chance for cross browser compatibility in parsing the date string so it is better to parse it like
var ds = "2014-03-26 10:52:00";
var dateArray = ds.split(" "); // split the date and time
var ds1 = dateArray[0].split("-"); // split each parts in date
var ds2 = dateArray[1].split(":"); // split each parts in time
var newDate = new Date(ds1[0], (+ds1[1] - 1), ds1[2], ds2[0], ds2[1], ds2[2]).getTime(); //parse it
var currentDate = new Date().getTime();
var diff = currentDate - newDate;
console.log(diff); //timestamp difference
You can use MomentJS library
var user_submited_time = moment('2014-03-26 10:52:00');
var now = moment();
var value = user_submited_time - now;
I need to calculate difference between two calendar dates. I have gone through various posts but the value returned is not correct.
Heres' my code:-
function getTotalDays()
{
var date11 = document.getElementById("departure_date").value;
var date22 = document.getElementById("arrival_date").value;
var one_day=1000*60*60*24;
var date1 = new Date(date11);
var date2 = new Date(date22);
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
var diffDays = Math.round(difference_ms/one_day);
alert(diffDays);
}
suppse the difference is 2 days its showing as 59.
What's wrong..??
The values you are passing to the date object are likely wrong. Its probably easier for you to do something like this:
var date1 = getDate(date11);
var date2 = getDate(date22);
with getDate being:
function getDate(date) {
//date format dd/mm/yyyy
var dateArr = date.split('/');
var date = new Date(dateArr[2], dateArr[1], dateArr[0]);
return date;
}