The date is particularly familiar in Javascript. It might be simple if you understand what to watch out for. We ensure you can expand your horizon about everything related to the Javascript Dates. Let’s get started with more useful information below.
Create a Javascript Date
Users might create a date through the leading ways below.
Use date arguments
Here are the following arguments that you have to use:
- Year: It displays the 4-digit year
- Month: from 0 to 11. The month is assigned as zero-indexed.
- Day: from 1 to 31.
- Hour: from 0 to 23.
- Minutes: from 0 to 59.
- Seconds: from 0 to 59.
- Milliseconds: from 0 to 999.
Now, we will present illustrated examples to make you clear about it.
var varDate = new Date(2022, 9, 23)
console.log(varDate ); // Sun Oct 23 2022 00:00:00 GMT+0700 (Indochina Time)
varDate = new Date(2022, 9, 23, 10)
console.log(varDate ); // Sun Oct 23 2022 10:00:00 GMT+0700 (Indochina Time)
varDate = new Date(2022, 9, 23, 10, 20)
console.log(varDate ); // Sun Oct 23 2022 10:20:00 GMT+0700 (Indochina Time)
varDate = new Date(2022, 9, 23, 10, 20, 40)
console.log(varDate ); // Sun Oct 23 2022 10:20:40 GMT+0700 (Indochina Time)
The results above will return to the local time. Besides, as for the case of UTC, users can do it like below.
new Date(Date.UTC(2022, 9, 23))
Notes:
There are double timezones in Javascript.
- Local time: display the timezone your personal computer is in.
- UTC: involved in GMT (Greenwich Mean Time)
Use without arguments
You need to get a date with the current time (local time) in case you want to use it without arguments.
var varDate = new Date()
console.log(varDate);
// The result is: Fri Sep 23 2022 12:28:45 GMT+0700 (Indochina Time)
Use the Timestamps
A Timestamp is the milliseconds lasted since the first day in 1970. Here is a typical example that demonstrates this case.
// Tue Jun 11 2019 07:00:00 GMT+0700 (Indochina Time)
new Date(1560211200000)
Use the dateString method
You can create a new date from the date string. Let’s see the example below.
var varDate = new Date("September 23, 2022 20:15:03")
console.log(varDate);
The result is:
Fri Sep 23 2022 20:15:03 GMT+0700 (Indochina Time)
Format a Javascript Date
A Date object goes with typical formatting methods. One of them offers you a particular value
- toString shows: Sun Jan 23, 2022, 17:25:45 GMT+0700
- toDateString shows: Sun Jan 23, 2022
- toLocaleString shows: 23/01/2022, 17:25:45
- toLocaleDateString shows: 23/01/2022
- toGMTString shows: Sun, 23 Jan 2022, 10:25:45 GMT
- toUTCString shows: Sun, 23 Jan 2022, 10:25:45 GMT
Next, before customizing the format, you have to make it first.
Custom date format
Suppose the case you have: Fri, 23 September 2022. You apply the date methods to create the value. To do it, you take the four methods below.
- getFullYear: It reflects the 4-digit year.
- getMonth: the value is from 0 to 11.
- getDate: the value is from 1 to 31.
- getDay: the value is from 0 to 6.
Let’s see the example below to know more.
const date = new Date(2022, 9, 23)
const year = date.getFullYear()
const d = date.getDate()
console.log(year); // 2022
console.log(d); // 23
Next, you should create the object that presents the month’s value.
const months = {
0: 'Jan',
1: 'Feb',
2: 'Mar',
3: 'Apr',
4: 'May',
5: 'Jun',
6: 'Jul',
7: 'Aug',
8: 'Sep',
9: 'Oct',
10: 'Nov',
11: 'Dec'
}
Then, use the array to generate similar outcomes.
const months = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
Implement getMonth to obtain the month from a date.
const monthName = months[d.getMonth()]
console.log(monthName) // September
Similarly, you can get ‘Thu’ by using an array like the one below.
const days = [
'Sun',
'Mon',
'Tue',
'Wed',
'Thu',
'Fri',
'Sat'
]
Next, you get dayName by using dayIndex.
const dayName = days[d.getDay()] // Fri
Finally, combine the variables above you made.
const formatted = `${dayName}, ${monthName} ${date} ${year}`
console.log(formatted) // Fri, September 23 2022
Compare Javascript Dates
The first thing you need to do is to compare dates with >, <, >=, and <=.
const now = new Date(2022, 9, 23)
const next = new Date(2022, 9, 24)
console.log(now < next) // true
Next, you could use getTime to check the timestamps when double dates drop simultaneously.
const checkTime = (varDate1, varDate2) => {
return varDate1.getTime() === varDate2.getTime()
}
const now = new Date(2022, 9, 23)
const today = new Date(2022, 9, 23)
console.log(checkTime(now, today)) // true
now_time = new Date(2022, 9, 23, 10)
today_time = new Date(2022, 9, 23, 10,30)
console.log(checkTime(now_time, today_time)) // fasle
Lastly, you could use the getDate, getMonth, and getFullYear if you need to know whether double dates drop on a similar day.
const checkTime = (varDate1, varDate2) => {
return varDate1.getFullYear() === varDate2.getFullYear() &&
varDate1.getMonth() === varDate2.getMonth() &&
varDate1.getDate() === varDate2.getDate()
}
const now = new Date(2022, 9, 23)
const today = new Date(2022, 9, 23)
console.log(checkTime(now, today)) // true
now_time = new Date(2022, 9, 23, 10)
today_time = new Date(2022, 9, 23, 10,30)
console.log(checkTime(now_time, today_time)) // true
Tutorials about dates in JS
See more other articles related to JS Date:
- Get the current year for copyright using javascript
- Add Hours to a Date in JavaScript
- Set the current Date on an Input type=”date” in JavaScript
- Convert a dd/mm/yyyy string to a Date in JavaScript
- Get yesterday’s Date formatted as YYYY-MM-DD in JavaScript
- Get the Sunday of the Current Week using JavaScript
- Format a date as yyyymmdd using javascript
- Format a date as yyyy-mm-dd in javascript
- Initialize a date with time zone using javascript
- Get the current year using javascript
- Convert a date to another time zone using javascript
- Get the First Day of a Month in JavaScript
- Add Hours to a Date object Using JavaScript
- Change getDate() to 2 digit format in JavaScript
- Change getMonth() to 2 digit format in JavaScript
- Check if a Date is the First day of the Month in JavaScript
- Get First and Last Day of the Current Month in JavaScript
- Validate a Date formatted as DD/MM/YYYY in JavaScript
- Increment a Date by 1 Month using JavaScript
- Get the GMT Hours using JavaScript
- Get the current Date and Time in Seconds using JavaScript
- Add Seconds to a Date using JavaScript
- Check if a Date is Yesterday using JavaScript
- Convert seconds to HH:MM:SS in JavaScript
- Get Year, Month and Day from a Date Object in JavaScript
- Format a Date as MM/DD/YYYY hh:mm:ss in JavaScript
- Format a Date as YYYY-MM-DD hh:mm:ss in JavaScript
- Get a GMT timestamp using JavaScript
- Get a UTC timestamp using JavaScript
- Get Date and Time in User’s locale format in JavaScript
- Get the Day Name from a specific Date using JavaScript
- Initialize a Date to Midnight using JavaScript
- Remove the Time from a Date using JavaScript
- Subtract Years from a Date in JavaScript
- Validate a Date formatted as DD/MM/YYYY in JavaScript
- Validate a Date formatted as MM/DD/YYYY in JavaScript
- Get Month and Date in 2 Digit Format in JavaScript
- Get the Start and End of the Day using JavaScript
- Get ISO Date without the Milliseconds using JavaScript
- Get the Number of Minutes between 2 Dates in JavaScript
- Subtract Months from a Date in JavaScript
- Convert a date to utc using javascript
- Get the month name from a date in javascript
- Check If A Date Is Between Two Dates In JavaScript
- Check If DST (Daylight Saving Time) Is In Effect In JavaScript
- Convert Seconds To Minutes And Seconds In JavaScript
- How To Compare two Date strings in JavaScript
- How To Convert Milliseconds To A Date Using JavaScript
- How To Convert Milliseconds to Minutes and Seconds in JavaScript
- How To Format a Date as DD/MM/YYYY in JavaScript
- How To Get First And Last Day Of The Current Week Using JavaScript?
- How To Get The Saturday Of The Current Week In JavaScript
- How To Get the Number of Days in a Month in JavaScript
- Get the first Day of the current Week using JavaScript
- Clone a Date object in JavaScript
- Convert a Date to GMT in JavaScript
- Get the Hours and Minutes from a Date in JavaScript
- Get tomorrow’s Date formatted as YYYY-MM-DD using JavaScript
- Get the Min/Max Dates in an Array in JavaScript
- How to parse a JSON Date using JavaScript
- Use toLocaleTimeString() without displaying Seconds in JavaScript
- Compare two Time strings formatted as HH:MM:SS in JavaScript
- Change getMinutes() to 2 digit format in JavaScript
- Get a short Locale Date string in JavaScript
- Convert Milliseconds to Hours, Minutes, Seconds using JavaScript
- Set a Date’s hours, minutes and seconds to 0 using JavaScript
- Remove the Seconds and Milliseconds from a Date in JavaScript
- Subtract Minutes from a Date in JavaScript
- Convert a Full Date to a Short Date in JavaScript
- Set only the Date ignoring Time in JavaScript
- Increment a Date by 7 Days in JavaScript
- Convert HH:MM:SS to Seconds using JavaScript
- Convert Minutes to Hours and Minutes in JavaScript
- Get the Number of Years between 2 Dates in JavaScript
- Check if a Year is a Leap Year using JavaScript
- Convert UTC to local time using JavaScript
- Convert an ISO string to a Date object in JavaScript
- Check if a Date is before Today’s date in JavaScript
- Convert a Date object to Ticks using JavaScript
- Get the First Day of Next Month in JavaScript
- Remove the local Time Zone from a Date using JavaScript
- Convert DD-MM-YYYY to YYYY-MM-DD format in JavaScript
- Change getHours() to 2 digit format in JavaScript
- Change getSeconds() to 2 digit format in JavaScript
- Convert Days to Seconds using JavaScript
- Check if a Date is after another Date using JavaScript
- Convert GMT to local time using JavaScript
- Convert a Date to Pacific Standard time (PST) in JavaScript
- How to get the Day of the Month in JavaScript
- Get the First Day of a Year using JavaScript
- Get the First day of the Previous Month in JavaScript
- Get the Time Zone Abbreviation using JavaScript
- Create a Date without Timezone using JavaScript
- Get the Quarter from a Date using JavaScript
- Check if a Date is during the Weekend using JavaScript
- Date.getDay() returns wrong day in JavaScript
- Convert Month number to Month name using JavaScript
- Create a Date from day, month, year using JavaScript
- getDate() returns the wrong date in JavaScript [Solved]
- Check if a Date is less than 1 Hour ago using JavaScript
- Check if a Date is less than 24 Hours ago in JavaScript
- Subtract Days from a Date in JavaScript
- Convert Seconds to Days, Hours, Minutes and Seconds in JS
- Get all Dates in a Month using JavaScript
- Convert a Timestamp to Relative Time (time ago)
- Get the Day of the Year (1 – 366) in JavaScript
Summary
We believe you can find it useful to learn more about the Javascript Dates through this post. If you expect to know more about it, please feel free to contact us. Thank you for reading.
My name is Robert. I have a degree in information technology and two years of expertise in software development. I’ve come to offer my understanding on programming languages. I hope you find my articles interesting.
Job: Developer
Name of the university: HUST
Major: IT
Programming Languages: Java, C#, C, Javascript, R, Typescript, ReactJs, Laravel, SQL, Python