Javascript timestamp



Keywords: javascript timestamp
Description: I often need to convert UNIX timestamps from log files into a human readable format and need a quick and easy to access tool for doing so, and have written up this post with the converter tool followed by an explanation of the Javascript code behind it.

The input field has an id "inputTimestamp" and the three table cells where the output is displayed have ids "outputDatabase, "outputLocal" and "outputUTC".

There is padding done on the numbers in the database format output for the month, day, hours minutes and seconds using the pad function supplied by Hans Pufel available here .

Line 2 stores a reference to the timestamp field as a variable because we'll be using it twice. Note that I'm just using regular old Javascript instead of a library like jQuery.

Line 3 then runs parseInt to get a number from the value and line 4 checks to see it is a valid number and that the parsed number is the same as the original number. This needs to be done otherwise for example parseInt will convert 131a7075506 to 131 which is a valid number, but not really valid for our purposes.

Line 10 creates a date object by multiplying the timestamp value by 1000 so it's in milliseconds - UNIX timestamps are in seconds.

The remaining lines then put the output into the table cells. I've used innerHTML because it's easy and supported by all browsers, and it appears to be in the HTML5 spec (whereas I'm not sure if it was an official part of earlier HTML versions).

When the page first loads it initializes the input timestamp with the current timestamp. The Javascript code for doing this is below:

Calling getTime() on the date object returns the current time in milliseconds, so it needs to be divided by 1000 to get the UNIX timestamp in seconds. Math.floor is used to round it down to the nearest whole number.






Photogallery Javascript timestamp:


687474703a2f2f692e696d6775722e ...


google_sheet.png


Screenshot_8.png


Convert Facebook Post ID To Unix Time Stamp - JavaScript | Dream ...


date.png


Using the Console - Google Chrome


PL/SQL Timestamp/Interval Datatypes (iPad) reviews at iPad Quality ...


PL/SQL Timestamp/Interval Datatypes (iPad) reviews at iPad Quality ...


rfd900x43drtoolerror.jpg


Javascript Get Date From Timestamp


frbtable_date.png


PL/SQL Timestamp/Interval Datatypes (iPad) reviews at iPad Quality ...


RS0gz.png


MYSQL - Order timestamp values ascending in order, from newest to ...


Coveloping


Testing JavaScript applications with sinon.js | Fourword: The Four ...


Geek Meet - Creating Games with HTML5 and JavaScript


Hit timestamps in Google Analytics | TZ Organic


Console API Reference - Google Chrome


Point-In-Time Queries (Application Developer's Guide)  MarkLogic ...