Hi Guys, Welcome to Proto Coders Point, In this Article will learn about javascript date & time, How to gte current date time in javascript, get time in IST, EST, CST format & how to remove timezone T & Z from datatime string in js.
What is Javascript Time?
System Time can we fetched in js using Date object, javascript return date in milliseconds number (since JAN 01, 1970 UTC), so we can make use of js date methods to get day, hours, minutes, seconds.
Syntax
const date = new Date(); console.log("current Time", date); // 2022-01-01T11:39:52.989Z
Javascript date methods
Here are few pre-defined js date methods to get date-time information from date object.
const date = new Date();
Method | Description | Code Example |
Date.now() | Get System Time. ECMAScript 5. | console.log(“Date Now: “,Date.now()); |
getDay() | Return weekday in number (0-6) | console.log(“Day: “,date.getDay()); |
getTime() | Return time in milliseconds. | console.log(“Time: “,date.getTime()); |
getMilliseconds() | Return milliseconds (0-999) | console.log(“Time Millisecond:”,date.getMilliseconds()); |
getSeconds() | Return seconds (0-59) | console.log(“Time Seconds: “,date.getSeconds()); |
getMinutes() | Return minute (0-59) | console.log(“Time Minute: “,date.getMinutes()); |
getHours() | Return Hours (0-23) | console.log(“Time Hours:”,date.getHours()); |
getDate() | Return day in number (1-31) | console.log(“Date:”,date.getDate()); |
getMonth() | Return Month in number (0-11) | console.log(“Current Month: “,date.getMonth()); |
getFullYear() | Return Current Year (YYYY) | console.log(“Current Year: “,date.getFullYear()); |
Get current time in IST javascript
The below snippet code will return time in IST format.
const date = new Date(); console.log("current Time", date); var ISToffSet = 330; //IST is 5:30; i.e. 60*5+30 = 330 in minutes offset= ISToffSet*60*1000; var ISTTime = new Date(date.getTime()+offset); console.log("IST Date",ISTTime);
JS get current time in EST
The below code will return you time in EST Time format
const date = new Date(); var offset = -300; //Timezone offset for EST in minutes. var estDate = new Date(date.getTime() + offset*60*1000); console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)
Get current time in CST javascript
The below code will return time in CST time now.
const date = new Date(); var CSToffSet = -360; //CST is -6:00 of UTC; i.e. 60*6 = -360 in minutes offset= CSToffSet*60*1000; var CSTTime = new Date(date.getTime()+offset); console.log("CST Date",CSTTime);
Javascript remove timezone offset from date
const date = new Date(); console.log(date);
As you know, date has a fixed length of character, therefore you can simple slice, as per your date requirement, here is what you can do.
//remove time zone let date_without_timezone = date.toISOString().slice(0,23); console.log("Date, Time without time zone: ", date_without_timezone);
Remove T and Z from date javascript
if you want to maintain the data format in js & only remove T & Z from time string then you can make use of replace() method. check the example below
//remove T and Z from time var timeTZremove = date.toISOString().replace('T',' ').replace('Z',' '); console.log("dateTime removed T Z: ",timeTZremove);
How to get full month name in javascript
To get Month full name in js, you can do as below
- Will create/declare a array variable that have list of month name.
const monthsList = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
- Then by using, javascript date methods as listed above, will use getMonth() method that return current month in number i.e. from 0-11.
let monthName = monthsList[date.getMonth()];
complete code – Get month name from Date
// get month full name in JS const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; let monthName = months[date.getMonth()]; console.log("Current Month: ", monthName);