Unix timestamp to date in javascript

How to Convert a UNIX Timestamp to a Date in JavaScript or Node.js

The UNIX time describes a point in time using an integer value. This integer is the number of seconds since the UNIX epoch. The UNIX epoch is 01 January 1970 at 00:00:00 UTC.

This tutorial shows you how to convert a UNIX timestamp to a JavaScript date instance.

Node.js Series Overview

  1. Increase the Memory Limit for Your Process
  2. Why You Should Add “node” in Your Travis Config
  3. Create a PDF from HTML with Puppeteer and Handlebars
  4. Create Your Own Custom Error
  5. Retrieve a Request’s IP Address in Node.js
  6. Detect the Node.js Version in a Running Process or App
  7. How to Base64 Encode/Decode a Value in Node.js
  8. Check if a Value Is Null or Undefined in JavaScript or Node.js
  9. How to Fix “Uncaught SyntaxError: Cannot use import statement outside a module”
  10. Fix „Socket Hang Up“ Errors
  11. Nested Destructuring in JavaScript or Node.js
  1. String Replace All Appearances
  2. Remove All Whitespace From a String in JavaScript
  3. Generate a Random ID or String in Node.js or JavaScript
  4. Remove Extra Spaces From a String in JavaScript or Node.js
  5. Remove Numbers From a String in JavaScript or Node.js
  6. Get the Part Before a Character in a String in JavaScript or Node.js
  7. Get the Part After a Character in a String in JavaScript or Node.js
  8. How to Check if a Value is a String in JavaScript or Node.js
  9. Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
  10. Check if a Value is a String in JavaScript and Node.js
  11. Limit and Truncate a String to a Given Length in JavaScript and Node.js
  12. Split a String into a List of Characters in JavaScript and Node.js
  13. How to Generage a UUID in Node.js
  14. Reverse a String in JavaScript or Node.js
  15. Split a String into a List of Lines in JavaScript or Node.js
  16. Split a String into a List of Words in JavaScript or Node.js
  17. Detect if a String is in camelCase Format in Javascript or Node.js
  18. Check If a String Is in Lowercase in JavaScript or Node.js
  19. Check If a String is in Uppercase in JavaScript or Node.js
  20. Get the Part After First Occurrence in a String in JavaScript or Node.js
  21. Get the Part Before First Occurrence in a String in JavaScript or Node.js
  22. Get the Part Before Last Occurrence in a String in JavaScript or Node.js
  23. Get the Part After Last Occurrence in a String in JavaScript or Node.js
  24. How to Count Words in a File
  25. How to Shuffle the Characters of a String in JavaScript or Node.js
  26. Append Characters or Words to a String in JavaScript or Node.js
  27. Check if a String is Empty in JavaScript or Node.js
  28. Ensure a String Ends with a Given Character in JavaScript or Node.js
  29. Left-Trim Characters Off a String in JavaScript or Node.js
  30. Right-Trim Characters Off a String in JavaScript or Node.js
  31. Lowercase the First Character of a String in JavaScript or Node.js
  32. Uppercase the First Character of a String in JavaScript or Node.js
  33. Prepend Characters or Words to a String in JavaScript or Node.js
  34. Check if a String is a Number
  35. Convert a String to Buffer
  1. Get Number of Seconds Since Epoch in JavaScript
  2. Get Tomorrow’s Date in JavaScript
  3. Increase a Date in JavaScript by One Week
  4. Add Seconds to a Date in Node.js and JavaScript
  5. Add Month(s) to a Date in JavaScript or Node.js
  6. Add Week(s) to a Date in JavaScript or Node.js
  7. Get the Current Year in JavaScript or Node.js
  8. How to Get a UNIX Timestamp in JavaScript or Node.js
  9. How to Convert a UNIX Timestamp to a Date in JavaScript or Node.js
  10. Add Days to a Date in JavaScript or Node.js
  11. Get Yesterday’s Date in JavaScript or Node.js
  12. Add Minutes to a Date in JavaScript or Node.js
  13. Add Hours to a Date in JavaScript or Node.js
  14. Check If a Date Is Today in JavaScript or Node.js
  15. Check If a Date is Tomorrow in JavaScript or Node.js
  16. Check If a Date is Yesterday in JavaScript or Node.js
  17. How to Format a Date YYYY-MM-DD in JavaScript or Node.js
  1. How to Run an Asynchronous Function in Array.map()
  2. How to Reset and Empty an Array
  3. for…of vs. for…in Loops
  4. Clone/Copy an Array in JavaScript and Node.js
  5. Get an Array With Unique Values (Delete Duplicates)
  6. Sort an Array of Integers in JavaScript and Node.js
  7. Sort a Boolean Array in JavaScript, TypeScript, or Node.js
  8. Check If an Array Contains a Given Value in JavaScript or Node.js
  9. Add an Item to the Beginning of an Array in JavaScript or Node.js
  10. Append an Item at the End of an Array in JavaScript or Node.js
  11. How to Exit and Stop a for Loop in JavaScript and Node.js
  12. Split an Array Into Smaller Array Chunks in JavaScript and Node.js
  13. How to Get an Index in a for…of Loop in JavaScript and Node.js
  14. How to Exit, Stop, or Break an Array#forEach Loop in JavaScript or Node.js
  15. Retrieve a Random Item From an Array in JavaScript or Node.js
  16. How to Reverse an Array in JavaScript and Node.js
  17. Sort an Array of Strings in JavaScript, TypeScript or Node.js
  18. Sort an Array of Objects in JavaScript, TypeScript or Node.js
  19. Check If a Value Is an Array in JavaScript or Node.js
  20. Join an Array of Strings to a Single String Value
  1. How to Merge Objects
  2. How to Check if an Object is Empty in JavaScript or Node.js
  3. How to CamelCase Keys of an Object in JavaScript or Node.js
  4. How to Snake_Case Keys of an Object in JavaScript or Node.js
  5. How to Destructure a Dynamic Key in JavaScript or Node.js
  6. How to Get All Keys (Including Symbols) from an Object in JavaScript or Node.js
  7. How to Delete a Key From an Object in JavaScript or Node.js
  8. Iterate Through an Object’s Keys and Values in JavaScript or Node.js
  9. How to Convert URLSearchParams to Object
  10. Check If a Value Is an Object in JavaScript or Node.js
  11. Conditionally Add Properties to an Object in JavaScript or Node.js
  12. How to Lowercase Keys of an Object in JavaScript or Node.js
  1. Get a File’s Created Date
  2. Get a File’s Last Modified or Updated Date of a File
  3. How to Create an Empty File
  4. Check If a Path or File Exists
  5. How to Rename a File
  6. Check If a Path Is a Directory
  7. Check If a Path Is a File
  8. Retrieve the Path to the User’s Home Directory
  9. How to Touch a File
  10. Read File Content as String
  11. Check If a Directory Is Empty
  12. How to Create a Directory (and Parents If Needed)
  13. Get a File‘s Extension
  14. Get the Size of a File
  15. Get a File Name (With or Without Extension)
  16. Read a JSON File
Читайте также:  Num rows php параметры

Convert UNIX Time to a Date in JavaScript

The UNIX time uses seconds to represent a point in time. JavaScript dates work with milliseconds. You can still translate a UNIX timestamp to a JavaScript by translating the given value from seconds to milliseconds:

/** * Returns a date instance from the given `unixTimestamp`. * * @param unixTimestamp Number of seconds since Unix epoch * * @returns */ function toDate (unixTimestamp)

Enjoy working with UNIX times and JavaScript dates!

Get Notified on New Future Studio
Content and Platform Updates

Get your weekly push notification about new and trending
Future Studio content and recent platform enhancements

Источник

Convert a Unix Timestamp to a Date in Vanilla JavaScript

How do you convert a Unix timestamp value into a human-readable date using vanilla JavaScript?

You can convert the Unix timestamp to a date string by following these three steps:

  1. Convert the unix timestamp into milliseconds by multiplying it by 1000 .
  2. Use the newly created milliseconds value to create a date object with the new Date() constructor method.
  3. Use the .toLocaleString() function to convert the date object into human-friendly date strings.

In this article, we’ll walk you through each of those steps.

Table of Contents

Convert the Unix Timestamp to Milliseconds

Since the new Date() function needs to be supplied with a milliseconds value, we need to first convert our given Unix timestamp to milliseconds. We can do this by simply multiplying the Unix timestamp by 1000 .

Unix time is the number of seconds that have elapsed since the Unix epoch, which is the time 00:00:00 UTC on 1 January 1970 . It’s most commonly used to create a running total of seconds when interacting with computers.

Therefore, a Unix timestamp is simply the number of seconds between a specific date and the original Unix Epoch date.

Measuring time using Unix timestamps is particularly useful because it is the same for everyone around the globe at all times since they don’t observe timezones. This can be very useful for dealing with dated information on both the server and client-side of applications.

So, let’s write some code to convert a Unix timestamp to milliseconds:

const unixTimestamp = 1575909015 const milliseconds = unixTimestamp * 1000 // 1575909015000 

Feel free to substitute your own Unix timestamp in the code above.

In the next section, we’ll put to use that milliseconds value we just created.

Create a Date Object Using new Date()

Now that we have a milliseconds value, we can create a new Date() object.

The Date object instance we create will represent a single moment in time and will hold data on the year, month, day, hour, minute, and second for that moment in time.

Let’s add on to the code we already wrote in the last section. To create the Date object, make your code look like this:

const unixTimestamp = 1575909015 const milliseconds = 1575909015 * 1000 // 1575909015000 const dateObject = new Date(milliseconds) 

We use the new Date() constructor and pass to it the milliseconds variable we created in the last section.

As a result, we’re left with a newly created dateObject variable that represents the Date object instance.

We’ll use this in the next section.

Create Human-Friendly Date Strings With .toLocaleString()

Now that we have a Date object to work with, we can start creating some human-friendly date strings.

Using the .toLocaleString() function is one really easy way to do this. The function can be called on a data object and will return a string with a language sensitive representation of the date portion of the given date object.

Here’s what a simple code example looks like (adding on to the code we have written in the past sections):

const unixTimestamp = 1575909015 const milliseconds = 1575909015 * 1000 // 1575909015000 const dateObject = new Date(milliseconds) const humanDateFormat = dateObject.toLocaleString() //2019-12-9 10:30:15 

As you can see, we created a human-friendly date string by calling the .toLocaleString() on the dateObject we created in the last section.

Here are some examples of how you can use the .toLocaleString() to return strings of specific components of the date by passing different arguments to the .toLocaleString() function:

const unixTimestamp = 1575909015 const milliseconds = 1575909015 * 1000 // 1575909015000 const dateObject = new Date(milliseconds) const humanDateFormat = dateObject.toLocaleString() //2019-12-9 10:30:15 dateObject.toLocaleString("en-US", ) // Monday dateObject.toLocaleString("en-US", ) // December dateObject.toLocaleString("en-US", ) // 9 dateObject.toLocaleString("en-US", ) // 2019 dateObject.toLocaleString("en-US", ) // 10 AM dateObject.toLocaleString("en-US", ) // 30 dateObject.toLocaleString("en-US", ) // 15 dateObject.toLocaleString("en-US", ) // 12/9/2019, 10:30:15 AM CST 

The .toLocaleString takes a locales string parameter that alters results based on language and geography. In the example above, we used the «en-US» locale tag. You can learn more about other values you can use instead here.

We also passed an object with some options in it as well. If you want to learn more, there’s some good information about those here.

Источник

Оцените статью