js: Date function: count number of day and format days
November 19, 2010 at 8:21 am Tinggalkan komentar
First function :
Use for format Date in string type.
//donairl : this function for change format dd/mm/yyyy into yyyy/mm/dd
// the reason i do this : because new Date can accept 'yyyy/mm/dd' to convert into dates
// other format :
// * MM-dd-yyyy
// * yyyy/MM/dd
// * MM/dd/yyyy
// * MMMM dd, yyyy
// * MMM dd, yyyy
// TODO : Can put custom date separator
function FormatDateStr(inputdt){
var temp = new Array();
temp = inputdt.split('/');
return temp[2]+'/'+temp[1]+'/'+temp[0];
}
Secondly this one i take from other person code, i don’t know who.
Use for count days between 2 dates.
Input is in Date Object.
If you have format string like this ’20/11/2010′
to convert to Date() , then use above function, so become like this :
var startDate = new Date(FormatDateStr($F(‘txtTgl1′)));
var endDate = new Date(FormatDateStr($F(‘txtTgl2′)));
numofdays = CountdaysBetween( startDate, endDate );
function CountdaysBetween(date1, date2) {
var DSTAdjust = 0;
// constants used for our calculations below
oneMinute = 1000 * 60;
var oneDay = oneMinute * 60 * 24;
// equalize times in case date objects have them
date1.setHours(0);
date1.setMinutes(0);
date1.setSeconds(0);
date2.setHours(0);
date2.setMinutes(0);
date2.setSeconds(0);
// take care of spans across Daylight Saving Time changes
if (date2 > date1) {
DSTAdjust =
(date2.getTimezoneOffset() - date1.getTimezoneOffset()) * oneMinute;
} else {
DSTAdjust =
(date1.getTimezoneOffset() - date2.getTimezoneOffset()) * oneMinute;
}
var diff = Math.abs(date2.getTime() - date1.getTime()) - DSTAdjust;
return Math.ceil(diff/oneDay);
}
Entry filed under: javascript. Tags: .
Trackback this post | Subscribe to the comments via RSS Feed