Converting UNIX timestamp to jQuery

Well I am getting the timestamp from my table using a php page with getJSON. Here's the structure.

main.php -> using getJSON (abc.php) -> value from my table

Is there a way to convert this UNIX timestamp to this format:

dd-mm-yyyy on hh: mm am

+3


source to share


3 answers


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

+6


source


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);
      

Run code


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>
      

Run code


Note. There are other libraries like moment.js that do things the same way too.

+2


source


var dt=eval(unixtimestamp*1000);
var myDate = new Date(dt);
return(myDate.toLocaleString());

      

this will give a result like: 10/27/2014, 12:58:45 PM

+1


source







All Articles