22
loading...
This website collects cookies to deliver better user experience
Date
in JavaScript remains one of the most complicated and thus, feared topics. So much so that we have to rely on external libraries like Moment.js for the simplest tasks.Date
object as per my understanding and the most common functionalities I use in day-to-day development. Let us get started...Date
in JavaScript. All are useful in their own ways. Let us look at them one by one and when you will most likely use them.const today = new Date();
console.log(today)
// Expected output: Tue Jun 29 2021 23:30:27 GMT+0530 (India Standard Time)
Date
object. This will create the Date
in the client's local timezone.createdDate
as a new Date()
.const someDate = new Date(1122334455);
console.log(someDate)
// Expected output: Wed Jan 14 1970 05:15:34 GMT+0530 (India Standard Time)
24 * 60 * 60 * 1000
(which is 24 hours in milliseconds), you will get the date as Jan 2nd of 1970 UTC+0.Date
because it is easier to communicate with the backend. When you want to save or retrieve dates, the backend prefers Unix Epoch Time.const someDate = new Date(2020, 11, 12, 18, 12, 50);
console.log(someDate)
// Expected output: Sat Dec 12 2020 18:12:50 GMT+0530 (India Standard Time)
Date
object by passing individual values of year, month, day, hours, minutes, seconds, and milliseconds.Date
constructor will take the default values for those parameters.Date
object. Because I would prefer passing milliseconds to the constructor most of the time.month
Integer value representing the month, beginning with 0 for January to 11 for December.const someDate = new Date('2020-04-11T10:20:30Z')
console.log(someDate)
// Expected output: Sat Apr 11 2020 15:50:30 GMT+0530 (India Standard Time)
Date
object by parsing the date string. This I feel is the most dangerous way of creating the date. Because we have to be careful of the format of the date string.YYYY-MM-DDTHH:mm:ss.sssZ
.Date
object, let us understand different methods on the date object. These common methods are useful in displaying dates or manipulating the dates. getFullYear()
returns the year part from the date. With setFullYear()
you can set the year part. Setting the year will not affect any other part of the date.getMonth()
returns month part from the date object. And with its counterpart setMonth()
we can set the month in the date.getDate()
returns the current date and setDate()
sets the date in the date object.var someDate = new Date();
console.log(someDate);
// Expected Output: Wed Jun 30 2021 09:02:03 GMT+0530 (India Standard Time)
// Gets the day of the week
console.log(someDate.getDay());
// Expected Output: 3
console.log(someDate.getFullYear());
// Expected Output: 2021
console.log(someDate.getMonth());
// Expected Output: 5
console.log(someDate.getDate());
// Expected Output: 30
someDate.setFullYear(2020);
console.log(someDate);
// Expected Output: Tue Jun 30 2020 09:13:00 GMT+0530 (India Standard Time)
someDate.setMonth(6)
console.log(someDate);
// Expected Output: Thu Jul 30 2020 09:13:33 GMT+0530 (India Standard Time)
someDate.setDate(31);
console.log(someDate);
// Expected Output: Fri Jul 31 2020 09:13:55 GMT+0530 (India Standard Time)
getHours()
, setHours()
, getMinutes()
, setMinutes()
, getSeconds()
, setSeconds()
, getMilliseconds()
and setMilliseconds()
are used for getting and setting hours, minutes, seconds and milliseconds respectively.var someDate = new Date();
console.log(someDate.getTime());
console.log(Date.now());
// Expected Output:
// 1625024936606
// 1625024936606
// Date.now() is same as saying someDate.getTime()
Date
object.const today = new Date(1625061658610);
const timeZone = 'America/New_York';
const dateOptions = {
timeZone: timeZone,
weekday: 'long',
month: 'long',
day: 'numeric',
hour12: true,
hour: '2-digit',
minute: '2-digit'
};
console.log(today.toLocaleString("en-US", dateOptions));
// Expected output: Wednesday, June 30, 10:00 AM
Date.toLocalString()
, please check this W3 School link