Timestamp to DateA 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 JavaScriptAll 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 JavaScript offers several functions to convert timestamps to date and time in a different format that humans can read. These functions are as follows:
Convert Timestamp to Date in JavaScriptTimestamp_to_date.html Output 2. Timestamp to Date in JavaIn 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 constructorBy passing the Timestamp object to Date constructor, we can convert the device current timestamp value to date. Consider the following example: Output b) Convert Timestamp to Date in Java Using Date ReferenceWe can even get the Date type by simply assigning the Timestamp object to Date reference. Output c) Convert Timestamp to Date in Java Using Calendar classThe 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 3. Timestamp to Date in PythonPython 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 timeThe module time gives many time-related methods; one of them is time that returns the number of seconds since epoch. Screenshot and output Get current timestamp using module datetimeThe 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 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 Convert timestamp to DateTime in PythonWe 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 Convert timestamp to formatted Date in PythonTo 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 Example 2: Screenshot and output 4. Timestamp to Date in PHPWe 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 PHPOutput timestamp using time(): 1610540287 timestamp using strtotime(): 1610540287 timestamp using mktime(): 1610540287 Get current timestamp in microtime using microtime() functionOutput 1610541098.9467 Get current timestamp using DateTime classPHP DateTime class provides an object oriented approach to manipulate date and time. Output 1610541454 1610541454 Convert current timestamp to date in PHPOutput 01/13/2021 01/13/2021 12:36:25 13/01/2021 Below are some different readable formats of date in PHP.
5. Timestamp to Date in MySQLIn 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:
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 Inserting timestamp records: Query and output Display all the records of a table: Query and output Syntax to convert timestamp to readable date/time: Convert timestamp to readable date/time: Query and output Syntax to convert the above timestamp to formatted date time: Convert timestamp to format date time: Query and output Next TopicCloud-Native Applications |