1. Getting Current Date and Time :
<10 p=""><10 p="">
function getDateandTime() {
var date = new Date(),
year = date.getFullYear(),
month = (date.getMonth() + 1).toString(),
formatedMonth = (month.length === 1) ? ("0" + month) : month,
day = date.getDate().toString(),
formatedDay = (day.length === 1) ? ("0" + day) : day,
hour = date.getHours().toString(),
formatedHour = (hour.length === 1) ? ("0" + hour) : hour,
minute = date.getMinutes().toString(),
formatedMinute = (minute.length === 1) ? ("0" + minute) : minute,
second = date.getSeconds().toString(),
formatedSecond = (second.length === 1) ? ("0" + second) : second;
return formatedDay + "-" + formatedMonth + "-" + year + " " + formatedHour + ':' + formatedMinute + ':' + formatedSecond;
}
Result is : 01-01-2016 08:40:13
2. Getting Current DateandTime with Two digits formate :
function getCurrentDate(){
var today = new Date();
var day = today.getDate() + "";
var month = (today.getMonth() + 1) + "";
var year = today.getFullYear() + "";
var hour = today.getHours() + "";
var minutes = today.getMinutes() + "";
var seconds = today.getSeconds() + "";
day = checkZero(day);
month = checkZero(month);
year = checkZero(year);
hour = checkZero(hour);
mintues = checkZero(minutes);
seconds = checkZero(seconds);
print(day + "/" + month + "/" + year + " " + hour + ":" + minutes + ":" + seconds);
function checkZero(data){
if(data.length == 1){
data = "0" + data;
}
return data;
}
}
Result is : 01/01/2016 08:40:13
3. Getting Current Standard Format (International format)
function js_yyyy_mm_dd_hh_mm_ss () {
now = new Date();
year = "" + now.getFullYear();
month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
day = "" + now.getDate(); if (day.length == 1) { day = "0" + day; }
hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour; }
minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second; }
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
Result is : 2016-01-01 08:21:34
4. Indian Time Zone current DateandTime
function getCurrentDate(){
var today = new Date();
var currentOffset = today.getTimezoneOffset();
var ISTOffset = 330; // IST offset UTC +5:30
today = new Date(today.getTime() + (ISTOffset + currentOffset)*60000);
var day = today.getDate() + "";
var month = (today.getMonth() + 1) + "";
var year = today.getFullYear() + "";
var hour = today.getHours() + "";
var minutes = today.getMinutes() + "";
var seconds = today.getSeconds() + "";
day = checkZero(day);
month = checkZero(month);
year = checkZero(year);
hour = checkZero(hour);
mintues = checkZero(minutes);
seconds = checkZero(seconds);
print(day + "/" + month + "/" + year + " " + hour + ":" + minutes + ":" + seconds);
print(day + "-" + month + "-" + year + " " + hour + ":" + minutes + ":" + seconds);
print(year + "-" + month + "-" + day + " " + hour + ":" + minutes + ":" + seconds);
function checkZero(data){
if(data.length == 1){
data = "0" + data;
}
return data;
}
}
Result is :
01/01/2016 13:20:13
01-01-2016 13:20:13
2016-01-01 13:20:13
5. How to get the Maximum Date from Array List10>10>
<10 p=""><10 p="">
10>10> <10 p=""><10 p="">
10>10> function max_date(all_dates) {
var max_dt = all_dates[0],
max_dtObj = new Date(all_dates[0]);
all_dates.forEach(function(dt, index)
{
if ( new Date( dt ) > max_dtObj){
max_dt = dt;
max_dtObj = new Date(dt);
}
});
return max_dt;
}
print(max_date(['2016-06-01','2016-08-01', '2016/09/01', '2016-02-02', '2016-05-03']));
Result is : 2016/09/01
<10 p=""><10 p="">
10>10>