How to get the Date and Time in Node
Table of Contents
In this post, we'll look at how to get the current date, including the month, year, day, hour, minute, and seconds, in Node.
We'll also look at how to get the date and time formatted in the YYYY-MM-DD
and YYYY-MM-DD HH:mm:ss
formats. This will all be done in vanilla JavaScript, without any third-party libraries.
Get the Current Date and Time
In Node, you can get the current date and time using the JavaScript Date
object. This is native to JavaScript and so comes with Node by default.
To get the current date and time, create a new Date
object.
JAVASCRIPTconst now = new Date();
From here, you can call several methods on the Date
object.
getDate()
: Returns the day of the month (from 1 - 31)getDay()
: Returns the day of the week (from 0 - 6)getMonth()
: Returns the month (from 0 - 11)getFullYear()
: Returns the year (four digits)getHours()
: Returns the hour (from 0 - 23)getMinutes()
: Returns the minutes (from 0 - 59)getSeconds()
: Returns the seconds (from 0 - 59)
Format the Date and Time
Using the above methods, we can format the date and time in a variety of ways.
Here is how to format a date in the YYYY-MM-DD
format.
JAVASCRIPTconst now = new Date();
const year = now.getFullYear();
const month = ("0" + (now.getMonth() + 1)).slice(-2);
const day = ("0" + now.getDate()).slice(-2);
// YYYY-MM-DD
const formatted = `${year}-${month}-${day}`;
Here is the same date and time formatted in the YYYY-MM-DD hh:mm:ss
format.
JAVASCRIPTconst now = new Date();
const year = now.getFullYear();
const month = ("0" + (now.getMonth() + 1)).slice(-2);
const day = ("0" + now.getDate()).slice(-2);
const hour = ("0" + now.getHours()).slice(-2);
const minute = ("0" + now.getMinutes()).slice(-2);
const second = ("0" + now.getSeconds()).slice(-2);
// YYYY-MM-DD hh:mm:ss
const formatted = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
Get the Current Timestamp
You can get the current timestamp in milliseconds since Unix epoch using the getTime()
method.
JAVASCRIPTconst now = new Date();
const timestamp = now.getTime();
You can then divide the timestamp by 1000
to get the number of seconds since the Unix epoch.
JAVASCRIPTconst now = new Date();
const timestamp = now.getTime();
const seconds = timestamp / 1000;
Another way to get the current timestamp is to use the Date.now()
method.
JAVASCRIPTconst timestamp = Date.now();
Get the Date and Time from Timestamp
Given a timestamp, you can pass it to the Date
constructor to get a new Date
object from it.
From there you can format the date object.
JAVASCRIPTconst now = new Date();
const milliseconds = now.getTime();
const date = new Date(milliseconds);
const year = date.getFullYear();
const month = ("0" + (date.getMonth() + 1)).slice(-2);
const day = ("0" + date.getDate()).slice(-2);
// YYYY-MM-DD
const formatted = `${year}-${month}-${day}`;
Conclusion
We've seen how to get the current date and time in Node. We've also seen how to format the date and time in any format you want, like YYYY-MM-DD
and YYYY-MM-DD hh:mm:ss
.
Hopefully, this post has been useful to you!
- Getting Started with Solid
- Getting Started with Svelte
- Getting Started with Electron
- How to build a Discord bot using TypeScript
- Using Puppeteer and Jest for End-to-End Testing
- Getting Started with Moment.js
- Learn how to build a Slack Bot using Node.js
- Creating a Twitter bot with Node.js
- Setting Up Stylus CSS Preprocessor
- Getting Started with Vuex: Managing State in Vue
- Using Axios to Pull Data from a REST API
- How To Create a Modal Popup Box with CSS and JavaScript