Javatpoint Logo
Javatpoint Logo

Timestamp to Date

A timestamp or time stamp is the current time when an event gets performed (or recorded) by a computer or digital camera. The current date and time are registered to a file and logs when data is created, executed, modified, or removed. A computer records the date and time when an action is taken over a document or folder. Another example of a timestamp is that a digital camera records the date and time of a photo when clicked.

We can convert a UNIX timestamp into human-readable date and time formats in different programming languages. Here, we will discuss how to convert timestamp to date in various programming languages.

1. Timestamp to Date in JavaScript

All the function that works with date and time in JavaScript uses a Date() object and its methods. The Date object offers different ways to get the machine's current timestamp in JavaScript. JavaScript gives and returns the machine timestamp result in microseconds since 1 January 1970 00:00:00 UTC.

Get the current time in JavaScript:

In JavaScript, when an object Date is created without any argument it uses the current time, so using the below code we will get the device current timestamp.

current_timestamp.html

Open the file on any supported browser and you will see the current timestamp.

Output

Timestamp to Date

JavaScript offers several functions to convert timestamps to date and time in a different format that humans can read. These functions are as follows:

Function Description
toDateString It converts the date portion of the Date object into a human-readable string.
toTimeString It converts the time portion of the Date object so that humans can read it.
toString It converts the Date object to a string value.
toLocaleString It converts the Date object to a string, using locale conventions.
toLocaleDateString It returns the only date of the Date object in string format, using locale conventions.
toLocaleTimeString It returns only the time of the Date object in string format, using locale conventions.
toJSON It returns the date as JSON data in string format.
toISOString It returns the date as a string, using the ISO standard.
toUTCString() It converts the Date object to a string based on universal time.

Convert Timestamp to Date in JavaScript

Timestamp_to_date.html

Output

Timestamp to Date

2. Timestamp to Date in Java

In Java, a timestamp can be easily converted to date using a constructor of java.util.Date class. A constructor of Date class takes a parameter of long value. So, it is require to convert a Timestamp object into a long value using getTime() method of java.sql.Timestamp class.

a) Convert Timestamp to Date in Java Using Date constructor

By passing the Timestamp object to Date constructor, we can convert the device current timestamp value to date. Consider the following example:

Output

Timestamp to Date

b) Convert Timestamp to Date in Java Using Date Reference

We can even get the Date type by simply assigning the Timestamp object to Date reference.

Output

Timestamp to Date

c) Convert Timestamp to Date in Java Using Calendar class

The getInstance() method of java.util.Calendar class is a static method, which is used with calendar object to get the instance of calendar based on the current time zone set by Java Runtime Environment.

Output

Timestamp to Date

3. Timestamp to Date in Python

Python has several modules like time, datetime and calendar that deal with various dates and times format. A UNIX timestamp passed out thousands of seconds between a particular date and time since January 1, 1970, at UTC. We can get these timestamps and convert them into the current date and time.

Get current timestamp using module time

The module time gives many time-related methods; one of them is time that returns the number of seconds since epoch.

Screenshot and output

Timestamp to Date

Get current timestamp using module datetime

The module datetime provides classes that manipulate date and time in an object-oriented way. One such module is datetime.datetime.now that returns the number of seconds since epoch in microseconds.

Screenshot and output

Timestamp to Date

Get current timestamp using module calendar:

Python also offers a calendar module to get the current timestamp. Using the calendar module, we will use the timegm method to get a timestamp. However, it is not recommended to use a calendar module because you will lose some milliseconds or nanoseconds.

Screenshot and output

Timestamp to Date

Convert timestamp to DateTime in Python

We will simply use the fromtimestamp method of a datetime module to get the date from UNIX timestamp. The method datetime.fromtimestamp() takes the timestamp value as input parameter and returns the result of local current date and time.

Screenshot and output

Timestamp to Date

Convert timestamp to formatted Date in Python

To get datetime in specific format you can use strftime method. The strftime() method convert datetime objects into specified string format.

Example 1:

Screenshot and output

Timestamp to Date

Example 2:

Screenshot and output

Timestamp to Date

4. Timestamp to Date in PHP

We can easily convert timestamp to date/time using inbuilt date(), time(), functions. An UNIX timestamp can be converted into any specified date format in PHP. There are lots of ways to get current timestamp value in PHP. These functions returns the date and time in specified string format.

time() : The time() function is one of the popular and widely used function to get current timestamp in PHP. This function doesn't require any argument to get timestamp value.

strtotime() : The strtotime() function is mostly used to get UNIX timestamp value. There are several supported date or time strings that can be passed as an argument in this function (such as "next Sunday", "+1 week", "+10 hours", "now" etc.). The "now" string parameter in strtotime() function returns current timestamp.

mktime(): The mktime() function is also used to return UNIX timestamp but it requires set of date parameters such as hour, minute, second, month, day, year.

Get current timestamp in PHP


Timestamp to Date

Output

timestamp using time(): 1610540287
timestamp using strtotime(): 1610540287
timestamp using mktime(): 1610540287

Get current timestamp in microtime using microtime() function

Output

1610541098.9467

Get current timestamp using DateTime class

PHP DateTime class provides an object oriented approach to manipulate date and time.

Output

1610541454
1610541454

Convert current timestamp to date in PHP


Timestamp to Date

Output

01/13/2021
01/13/2021 12:36:25
13/01/2021

Below are some different readable formats of date in PHP.

mm/dd/yyyy $date = date("m/d/Y", $current_timestamp);
dd/mm/yyyy $date = date("d/m/Y", $current_timestamp);
With time $date = date("d F Y H:i:s", $current_timestamp);
Without time $date = date("d F Y", $current_timestamp);

5. Timestamp to Date in MySQL

In MySQL, the FROM_UNIXTIME() function converts a timestamp value to date and time. The function FROM_UNIXTIME() returns different types of data/time result and its format is based on parameters passed in it. These queries are:

MySQL query Result
SELECT UNIX_TIMESTAMP(NOW()) (here now() is optional) returns the current epoch time
SELECT UNIX_TIMESTAMP(CURDATE()) returns today midnight date
SELECT UNIX_TIMESTAMP(DATE_ADD(CURDATE(),INTERVAL -1 DAY)) returns yesterday midnight date
SELECT UNIX_TIMESTAMP(CONCAT(YEAR(CURDATE()),'-01-01')) returns Jan 1 of the current year
SELECT UNIX_TIMESTAMP(timestring) it converts a date to epoch time, and its time format is YYYY-MM-DD HH:MM:SS or YYMMDD or YYYYMMDD
SELECT FROM_UNIXTIME(epoch timestamp, optional output format) it converts epoch time to date; the default out format is YYYY-MM-DD HH:MM:SS

Let's try this function in an example to convert timestamp to date or date/time. At first, we will create a simple table with a column of int type. After that, we convert it to timestamp and then into date/time.

Creating a table:

Query and output

Timestamp to Date

Inserting timestamp records:

Query and output

Timestamp to Date

Display all the records of a table:

Query and output

Timestamp to Date

Syntax to convert timestamp to readable date/time:

Convert timestamp to readable date/time:

Query and output

Timestamp to Date

Syntax to convert the above timestamp to formatted date time:

Convert timestamp to format date time:

Query and output

Timestamp to Date




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA