In this post, we are going to learn how to convert Timestamp to DateTime in JavaScript with example. First, we will learn how to get Unix timestamp in Javascript and convert this to datetime and different timezone.
How to Get Unix timestamp in Javascript
To convert a UNIX timestamp to date, the first step is to get the current timestamp in Javascript or even you can use a hard code value
2 ways to get Unix timestamp in Javascript
- By using the getTime()
- Get timestamp from a given date: we have to convert it into milliseconds by multiplying 1000.
Program to get unix Timestamp
const current_timestamp = new Date().getTime()
console.log(current_timestamp);
//timestamp from a date
function datetoTimestamp(strDate){
var date = Date.parse(strDate);
return date/1000;
}
timestamp = datetoTimestamp('10/19/2021, 4:21:54 ')
timestamp_to_milsec = timestamp*1000;
console.log('millsecond:',timestamp_to_milsec);
Output
1634659545467
millsecond: 1634617314000
1. Convert Timestamp to DateTime in JavaScript
Now we have got the UNIX timestamp in milliseconds the next step is to convert it to date and time. In the below example we get the datetime form by using date class methods
The date class methods help us get the date in different formats as per need.
- getDate():Use to get day of calendar month 1 to 31.
- getMonth(): Use to get month number 0 to 11.
- getFullYear(): Use to get year in 4-digits format.
- getHours():Use to get the hour in 24-hour format.
- getMinutes():Use to get the minutes 0 to 59.
- getSeconds() : Use to get seconds 0 to 59.
JavaScript Program to Unix timestamp to DateTime
In this example, we have used getime() method to get the UNIX timestamp in milliseconds and create an object of the date class and use its method to get datetime.
By using the hard code value instead of finding the current timestamp.
//get unixtimestamp usng gettime() method
const current_timestamp = new Date().getTime()
console.log(current_timestamp);
var date = new Date(current_timestamp);
console.log("Full date: "+date.getDate()+"/"+(date.getMonth()+1)+
"/"+date.getFullYear()+" "+date.getHours()+
":"+date.getMinutes()+":"+date.getSeconds());
//by using hard code value of timestamp
var millsecond = 1634660514*1000;
var date = new Date(millsecond);
console.log("Full date: "+date.getDate()+"/"+(date.getMonth()+1)+
"/"+date.getFullYear()+" "+date.getHours()+
":"+date.getMinutes()+":"+date.getSeconds());
Output
Full date: 19/10/2021 16:17:40
Full date: 19/10/2021 16:21:54
2. toDateString() to Convert timestamp to date in JavaScript
In this example, we are using the todateString() method to get the date from UNIX epoch time/unixtimestamp.
JS Program to convert unix timestamp to date
var millsecond = 1634617314*1000;
var date = new Date(millsecond).toDateString();
console.log(date)
Output
Tue Oct 19 2021
3. Convert timestamp to different timezone
To convert Unix timestamp tolocalstring() function is used to convert a date object to a string based on passed argument value convert date to a different timezone.
Synatx
tolocalstring(locales,options)
Parameters
- Locales: The locales is an array of locale strings that is language-specific format based on national geography. In the below example, we have used “en-US”.
- options: It conatins more property that represent comparsion options.
JS Program unix timestamp to different timezone
We can change the locales as per need from the below locales tables.
//get datetime
var millsecond = 1634660514*1000;
var datetime = new Date(millsecond).toLocaleString("en-US");
console.log('datetime=,datetime)
//get date only
var date = new Date(millsecond).toLocaleDateString("en-US");
console.log('date=',date)
//get time only
var time = new Date(millsecond).toLocaleTimeString("en-US");
console.log('time=',time)
//by using the second options parameters
var datetime = new Date(millsecond);
var datestr = datetime.toLocaleString();
console.log(datestr)
console.log(datetime.toLocaleString("en-US", {dateStyle: "full"}))
console.log(datetime.toLocaleString("en-US", {weekday: "long"}))
console.log(datetime.toLocaleString("en-US", {timeZoneName: "long"}))
console.log(datetime.toLocaleString("en-US", {hour: "2-digit"}))
Output
dateTime = 10/19/2021, 4:21:54 PM
date = 10/19/2021
Time = 4:21:54 PM
10/19/2021, 4:21:54 AM
Tuesday, October 19, 2021
Tuesday
10/19/2021, 4:21:54 AM Coordinated Universal Time
04 AM
Tables of locals for different timezone
ar-SA :Arabic (Saudi Arabia) bn-BD: Bangla (Bangladesh) bn-IN: Bangla (India) cs-CZ : Czech (Czech Republic) da-DK: Danish (Denmark) de-AT: Austrian German de-CH: “Swiss” German de-DE: Standard German (as spoken in Germany) el-GR: Modern Greek en-AU: Australian English en-CA: Canadian English en-GB: British English en-IE: Irish English en-IN: Indian English en-NZ: New Zealand English en-US: US English en-ZA: English (South Africa) es-AR: Argentine Spanish es-CL: Chilean Spanish es-CO: Colombian Spanish es-ES: Castilian Spanish (as spoken in Central-Northern Spain) es-MX: Mexican Spanish es-US: American Spanish fi-FI: Finnish (Finland) fr-BE: Belgian French fr-CA: Canadian French fr-CH: “Swiss” French | fr-FR: Standard French (especially in France) he-IL: Hebrew (Israel) hi-IN: Hindi (India) hu-HU:Hungarian (Hungary) id-ID : Indonesian (Indonesia) it-CH: “Swiss” Italian it-IT: Standard Italian (as spoken in Italy) ja-JP: Japanese (Japan) ko-KR :Korean (Republic of Korea) nl-BE: Belgian Dutch nl-NL: Standard Dutch (as spoken in The Netherlands) no-NO: Norwegian (Norway) pl-PL: Polish (Poland) pt-BR: Brazilian Portuguese pt-PT: European Portuguese (as written and spoken in Portugal) ro-RO : Romanian (Romania) ru-RU: Russian (Russian Federation) sk-SK :Slovak (Slovakia) sv-SE: Swedish (Sweden) ta-IN: Indian Tamil ta-LK: Sri Lankan Tamil th-TH: Thai (Thailand) tr-TR: Turkish (Turkey) zh-CN: “Mainland China, simplified characters zh-HK: Hong Kong, traditional characters zh-TW : Taiwan, traditional characters |
Summary
In this post, we have learned how to convert Timestamp to DateTime in JavaScript.how to get Timestamp. How to convert timestamps at different timezone.