- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to calculate local time in Node.js?
In this article, you will understand how to calculate local time in Node.js. The Date object works with dates and times. Date objects are created with new Date(). JavaScript will use the browser's time zone and display a date as a full text string.
Node.js is an open-source and cross-platform JavaScript runtime environment.As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.
Example 1
In this example, we use the toDateString and toTimeString function
const dateTimeObject = new Date(); console.log("A date-time object is created
") console.log(`Date: ${dateTimeObject.toDateString()}`); console.log(`Time: ${dateTimeObject.toTimeString()}`);
Output
A date-time object is created Date: Tue Jan 03 2023 Time: 11:27:40 GMT+0530 (India Standard Time)
Explanation
Step 1 − Define a date time object.
Step 2 − Display the date by using the toDateString() method.
Step 3 − Display the time by using the toTimeString() method.
Example 2
In this example, fetch the year, month etc separately using functions like getDate(), getMonth.
let dateObject = new Date(); console.log("A date object is defined") let date = ("0" + dateObject.getDate()).slice(-2); let month = ("0" + (dateObject.getMonth() + 1)).slice(-2); let year = dateObject.getFullYear(); let hours = dateObject.getHours(); let minutes = dateObject.getMinutes(); let seconds = dateObject.getSeconds(); console.log("\displaying date and time in yyyy-mm-dd format") console.log(year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds);
Output
A date object is defined displaying date and time in yyyy-mm-dd format 2023-01-03 11:34:44
Explanation
Step 1 − Define a date time object.
Step 2 − Get the year, month and date value with getDate, getMonth and getFullYear methods respectively.
Step 3 − Get the hours, minutes and seconds values with getHours, getMinutes and getSeconds methods respectively.
- Related Articles
- How to use time() with local time in Android sqlite?
- How to print local time in Android sqlite?
- How to convert UTC date time into local date time using JavaScript?
- How to get local time in android using localtime API class?
- How to get the current local date and time in Kotlin?
- How to calculate elapsed/execution time in Java?
- Convert the specified Windows file time to an equivalent local time in C#
- How to get local time and date in android using LocalDateTime API class?
- How to calculate time based on seconds in MySQL?
- How to calculate Elapsed time in OpenCV using C++?
- How to set local date/time in a table using LocalDateTime class in Java?
- Python program to convert local time into GMT
- Java Program to Convert the local Time to GMT
- Golang program to convert the local time to GMT
- How to get offsetdatetime local date in android using offset date time API class?
