Convert UNIX timestamp in jQuery

前端 未结 3 1386
不知归路
不知归路 2021-01-19 00:27

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 t

相关标签:
3条回答
  • 2021-01-19 01:09

    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.

    0 讨论(0)
  • 2021-01-19 01:24

    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

    0 讨论(0)
  • 2021-01-19 01:25
    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

    0 讨论(0)
提交回复
热议问题