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

5
  • What format is it stored in the database? Your PHP script itself can send the string Commented Feb 9, 2013 at 9:38
  • do you need the timestamp in the browser ? if no have php do this job Commented Feb 9, 2013 at 9:38
  • It is stored using mysql timestamp function. Commented Feb 9, 2013 at 9:39
  • @mikakun well I get several datas in several rows which I fetch using getJason method. Commented Feb 9, 2013 at 9:41
  • You have to use the procedure explained on this stackoverflow.com/questions/1056728/… and this stackoverflow.com/questions/3552461/… Commented Jun 19, 2014 at 10:17

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

not working i got Uncaught TypeError: Object [object Date] has no method 'format'
It's not working because you don't have jquery date format plugin
to create a unix time stamp from new Date() is Math.round((new Date()).getTime() / 1000)
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.