JavaScript : Date & Time

 


<<----Date & Time in JavaScript :---->>


1. Date Methods(get and set)
2. Time Method(get and set)

JavaScript Date object represent a single moment in time in a 
platform-independent format. Data onjects contain a number 
that represent milli-seconds since 1 january 1970 UTC.

❄ Creating Object
There are 4 way to create a new data object:

a. new Date()
b. new Date(year,month,day,hours,minutes,seconds,milliseconds)
it takes 7 arguments
c. new Date(milliseconds)
we cannot avoid month section
new Date(date string)



👉new Date()
Date object are created with the new Data() constructer

let currDate = new Date();
console.log(currDate);

console.log(new Date());
console.log(new Date().toLocaleString()); // 9/11/2019, 1;25:01 Pm
console.log(new Date().toString()); // Wed Sep 11 2019 13:25:01

👉Date.now()
Return the numeric value corresponding to the current time-the Number 
of milliseconds elapsed since january 11970 00:00:00 UTC

console.log(Date.now());

👉new Date(yearmonth,...)
7 number specify year,month,day,hour,minutes,second and mil-sec (in that order)
NoteJavaScript counts months from 0 to 11.
januay is 0 and Dec is 11.
Month argument is must

var d = new Date(2021,7,27,20,20,0);
console.log(d.toLocaleString());


👉new Date(dateString)
new Date(dateStringcreates a new object from a date string

var d = new Date("october 13, 2014 11:13:00");
console.log(d.toLocaleString());

Date Methods :-

const currDate = new Date();

How to get the invisible date.

console.log(currDate.toLocaleString())
console.log(currDate.getFullYear());
console.log(currDate.getMonth()); // 0 to 11 jan to dec
console.log(currDate.getDate());
console.log(currDate.getDay());

How to set the individual date.

console.log(currDate.setFullYear(2022));
console.log(currDate.setFullYear(2022,10,5));
console.log(currDate.setMonth(10));
console.log(currDate.setDate(5));
console.log(currDate.toLocaleString());


Time Methods :-

const curTime = new Date();

how to get the indivdual time 

console.log(curTime.getTime());
// The getTime() method return the number of milliseconds
// since january 1,1970
console.log(curTime.getHours());
// The getHours() method returns the hours of a date as a 
// number (0-23)
console.log(curTime.getMinutes());
console.log(curTime.getSeconds());
console.log(curTime.getMilliseconds());

How to get the invisible date.

let curTime = new Date();

console.log(curTime.setHours(5));
console.log(curTime.setMinutes(5));
console.log(curTime.setSeconds(5));
console.log(curTime.setMilliseconds(5));



Practise Time :-

new Date().toLocaleTimeString(); // 11:18:48 AM
new Date().toLocaleDateString(); // 11/16/2015
new Date().toLocaleString();     // 11/16/2015,11:18:48 PM