JS Date Object
Written By: Avinash Malhotra
Updated on
JS Date Object
JavaScript Date Object is used to get date and time of user device. Using JS date object, we can detect date, time, timezone, day and also change date time data using js date function. JS Date Object is a build in constructor object is JavaScript.
JS will pick date and time from user machine. The accuracy of date and time totally depends upon device. Its recommended to use automatic date and time.
new Date()
new Date() function is used to get date and time in javascript. Date is a constructor function using new operator. See example
new Date Object
var date=new Date(); // new Date Object
var date=Date(); // Date String
new Date from EPOCH
var date=new Date(0);
// new Date Object from EPOCH Date and Time
The EPOCH time is 1 Jan 1970 12:00 AM GMT.
new Date function with date
var date=new Date('2018 01 30');
// new Date Object with custom date
new Date function with date and time
var date=new Date('2018-01-30T14:00');
// new Date Object with custom date and time
new Date function with date, time and timezone
var date=new Date('2018-01-30T12:00:00Z');
// new Date Object with custom date, time & timezone
// Z stands for Greenwich Mean Time
If system date is incorrect, date function will respond incorrect date and time.
getDate()
The getDate() and getUTCDate() method of date object returns current date.
var t=new Date();
var date=t.getDate();
document.write(date);
getYear()
The getYear() method of date object returns year relative to 1900.
var t=new Date();
var year=t.getYear();
document.write(year);
getFullYear()
The getFullYear() method of date object returns actual year or absolute year.
var t=new Date();
var year=t.getFullYear();
document.write(year);
getMonth()
The getMonth() method of date object returns month in array. For January, getMonth() will return 0, and for December getMonth() will return 11.
var t=new Date();
var month=t.getMonth();
document.write(month);
getDay()
The getDay() method of date object returns day in array. For Sunday, getDay() will return 0, and for Saturday getDay() will return 6.
var t=new Date();
var day=t.getDay();
document.write(day);
getHours()
The getHours() method of date object returns hour in 24 hour format. For 12:00 AM, hour will return 0 and for 02:00 PM, hour will return 14.
var t=new Date();
var hour=t.getHours();
document.write(hour);
getMinutes()
The getMinutes() method of date object returns minute form 0 to 59.
var t=new Date();
var min=t.getMinutes();
document.write(min);
getSeconds()
The getSeconds() method of date object returns seconds form 0 to 59.
var t=new Date();
var sec=t.getSeconds();
document.write(sec);
Refresh Seconds value after every second
function changeSec(){
var t=new Date();
var sec=t.getSeconds();
document.querySelector("p").innerHTML=sec;
}
setInterval(changeSec,1000)
getMilliseconds()
The getMilliseconds() method of date object returns millisecond form 0 to 999.
function changeMilliSec(){
var t=new Date();
var msec=t.getMilliSeconds();
document.querySelector("p").innerHTML=msec;
}
setInterval(changeMilliSec,1)
getTime()
The getTime() method of date object returns total milliseconds from 1-Jan-1970 12:00 AM (Epoch Time) till now.
function getTime(){
var t=new Date();
var gt=t.getTime();
document.querySelector("p").innerHTML=gt;
}
setInterval(getTime,1)
toDateString()
The toDateString() method of date object returns Day, Month, Date and Year in String.
var t=new Date();
var dstr=t.toDateString();
document.querySelector("p").innerHTML=dstr;
toLocaleDateString()
The toLocaleDateString() method of date object returns locale date in String form, i.e. dd/mm/yyyy.
var t=new Date();
var ldstr=t.toLocaleDateString();
document.querySelector("p").innerHTML=ldstr;
toTimeString()
The toTimeString() method of date object returns time in String form, i.e. hh:mm:ss GMT ±nnnn(Time ZOne).
function timeStr(){
var t=new Date();
var tstr=t.toTimeString();
document.querySelector("p").innerHTML=tstr;
}
setInterval(timeStr,1)
toLocaleTimeString()
The toLocaleTimeString() method of date object returns locale time in String form, i.e. hh:mm:ss.
function timeStr(){
var t=new Date();
var ltstr=t.toLocaleTimeString();
document.querySelector("p").innerHTML=ltstr;
}
setInterval(timeStr,1)
toLocaleString()
The toLocaleString() method of date object returns locale date and time in String form, i.e. DD/MM/YYYY, hh:mm:ss.
function timeStr(){
var t=new Date();
var lstr=t.toLocaleString();
document.querySelector("p").innerHTML=lstr;
}
setInterval(timeStr,1)
toGMTString()
The toGMTString() method of date object returns date and time of GMT in String form, i.e. Day, dd Month YYYY HH:MM:SS GMT.
function timeStr(){
var t=new Date();
var gmtstr=t.toGMTString();
document.querySelector("p").innerHTML=gmtstr;
}
setInterval(timeStr,1)
toUTCString()
The toUTCString() method of date object returns date and time of UTC, same as GMT in String form, i.e. Day, dd Month YYYY HH:MM:SS GMT.
function timeStr(){
var t=new Date();
var utcstr=t.toUTCString();
document.querySelector("p").innerHTML=utcstr;
}
setInterval(timeStr,1)
toISOString()
The toISOString() method of date object returns date and time of ISO in ISO format, i.e. YYYY-MM-DDTHH:MM:SS.ms Z, where Z means GMT.
function timeStr(){
var t=new Date();
var isostr=t.toISOString();
document.querySelector("p").innerHTML=isostr;
}
setInterval(timeStr,1)