← servoy magazine

[Tip] Return number of days between dates

  • Tips

by Enrico Arata

Returns the number of days passed between date_1 to date_2:

// Global method date_compare(start_date, end_date)
// This method (you can drop it in your global methods) returns the number of days passed between date_1 to date_2
// ignoring the time part of the date
// Calling example:
// var num_days = globals.date_compare(d_start, d_end))
// you can use it to test if 2 dates are equal:
// if (globals.date_compare(d_start, d_end) == 0){ // same date }

var s1= new Date (utils.dateFormat(arguments[0],"yyyy MMM dd"))
var s2= new Date (utils.dateFormat(arguments[1],"yyyy MMM dd"))
var num_days = Math.floor((s2 - s1) / 864E5)
return (num_days)