Converting UNIX timestamp to jQuery
A Unix timestamp is the number of seconds since 1970 (epoch). You will need to convert this to a date object in JS:
var date = new Date(unixTimestamp*1000); // *1000 because of date takes milliseconds
Once you have a date object, you can use any of the methods mentioned in the following post: How to Format a JavaScript Date
source to share
To convert a UNIX time stamp to a given format dd-mm-yyyy at hh:mm am
; you must first create a JavaScript Date object and then use either the native JavaScript date methods or the jQuery date.format library.
Step 1: Create a Date Object
var timestampInMilliSeconds = unixTimeStamp*1000; //as JavaScript uses milliseconds; convert the UNIX timestamp(which is in seconds) to milliseconds.
var date = new Date(timestampInMilliSeconds); //create the date object
Step 2: Convert to Target Format
Option # 1 - Using built-in JavaScript date methods
var day = (date.getDate() < 10 ? '0' : '') + date.getDate(); //adding leading 0 if date less than 10 for the required dd format
var month = (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1); //adding leading 0 if month less than 10 for mm format. Used less than 9 because javascriptmonths are 0 based.
var year = date.getFullYear(); //full year in yyyy format
var hours = ((date.getHours() % 12 || 12) < 10 ? '0' : '') + (date.getHours() % 12 || 12); //converting 24h to 12h and using 12 instead of 0. also appending 0 if hour less than 10 for the required hh format
var minutes = (date.getMinutes() < 10 ? '0' : '') + date.getMinutes(); //adding 0 if minute less than 10 for the required mm format
var meridiem = (date.getHours() >= 12) ? 'pm' : 'am'; //setting meridiem if hours24 greater than 12
var formattedDate = day + '-' + month + '-' + year + ' at ' + hours + ':' + minutes + ' ' + meridiem;
Note. The date shown will be based on the customer's time zone. If you need UTC based time; use getUTCDate () , getUTCMonth () , getUTCFullYear () , getUTCHours () , getUTCMinutes () and getUTCHours () method of JavaScript Date object instead of provided one.
Option # 2 - Using the date.format jQuery library
-
Download date.format.js
https://raw.githubusercontent.com/jacwright/date.format/master/date.format.js
-
Include date.format.js in your HTML file
<script src="/script dir/date.format.js"></script> //substitute "script dir" with the directory where date.format.js reside
-
Format date object
You can use format string just like in PHP
var formattedDate = date.format('d-m-Y \\a\\t h:i a');
Examples of
Option number 1
var unixTimeStamp = 983112343;
var timestampInMilliSeconds = unixTimeStamp*1000;
var date = new Date(timestampInMilliSeconds);
var day = (date.getDate() < 10 ? '0' : '') + date.getDate();
var month = (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1);
var year = date.getFullYear();
var hours = ((date.getHours() % 12 || 12) < 10 ? '0' : '') + (date.getHours() % 12 || 12);
var minutes = (date.getMinutes() < 10 ? '0' : '') + date.getMinutes();
var meridiem = (date.getHours() >= 12) ? 'pm' : 'am';
var formattedDate = day + '-' + month + '-' + year + ' at ' + hours + ':' + minutes + ' ' + meridiem;
alert(formattedDate);
Option number 2
var unixTimeStamp = 983112343;
var timestampInMilliSeconds = unixTimeStamp*1000;
var date = new Date(timestampInMilliSeconds);
var formattedDate = date.format('d-m-Y \\a\\t h:i a');
alert(formattedDate);
<script src="https://raw.githubusercontent.com/jacwright/date.format/master/date.format.js"></script>
Note. There are other libraries like moment.js that do things the same way too.
source to share