Convert UNIX timestamp in jQuery
Asked Answered
B

3

6

Well, I fetch timestamp from my table using a php page with getJSON. Here is the structure.

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

Is there any way to convert this UNIX timestamp into this format:

dd-mm-yyyy at hh:mm am

Biathlon answered 9/2, 2013 at 9:35 Comment(5)
What format is it stored in the database? Your PHP script itself can send the stringHeaver
do you need the timestamp in the browser ? if no have php do this jobWurster
It is stored using mysql timestamp function.Biathlon
@Wurster well I get several datas in several rows which I fetch using getJason method.Biathlon
You have to use the procedure explained on this #1057228 and this #3552961Dickerson
U
12

The Unix timestamp is the number of seconds elapsed since 1970 (epoch). You would need to convert that to a date object in JS:

var date = new Date(unixTimestamp*1000); // *1000 because of date takes milliseconds

Once you have the date object, you can use any of the techniques mentioned in the following post: How to format a JavaScript date

Untie answered 9/2, 2013 at 9:44 Comment(3)
not working i got Uncaught TypeError: Object [object Date] has no method 'format'Lunette
It's not working because you don't have jquery date format pluginXeniaxeno
to create a unix time stamp from new Date() is Math.round((new Date()).getTime() / 1000)Freemon
S
4
var dt=eval(unixtimestamp*1000);
var myDate = new Date(dt);
return(myDate.toLocaleString());

this will give an output like:10/27/2014, 12:58:45 PM

Staff answered 24/12, 2014 at 10:17 Comment(0)
K
3

For converting UNIX timestamp to the given format dd-mm-yyyy at hh:mm am; you have to first construct a JavaScript Date object and then use either the native JavaScript Date methods or the date.format jQuery Library.

Step 1: Constructing 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: Converting to the Given Format

Option #1 - Using native 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 client timezone. If you need UTC based time; use getUTCDate(), getUTCMonth(), getUTCFullYear(), getUTCHours(), getUTCMinutes() and getUTCHours() methods of the JavaScript Date Object instead of the provided.

Option #2 - Using 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 the date object

    You can use the format string in a similar fashion as in PHP

    var formattedDate = date.format('d-m-Y \\a\\t h:i a');

Examples

Option #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 #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 which too does the stuff similarly.

Kean answered 9/7, 2015 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.