在 jQuery 中转换 UNIX 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14786736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:55:18  来源:igfitidea点击:

Convert UNIX timestamp in jQuery

jquerygetjson

提问by Jerry Jones

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

好吧,我使用带有 getJSON 的 php 页面从我的表中获取时间戳。这是结构。

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

main.php --> 使用 getJSON (abc.php) --> 表中的值

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

有什么方法可以将此 UNIX 时间戳转换为这种格式:

dd-mm-yyyy at hh:mm am

dd-mm-yyyy 在 hh:mm am

回答by Konstantin Pozhidaev

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

Unix 时间戳是自 1970 年(纪元)以来经过的秒数。您需要将其转换为 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

拥有日期对象后,您可以使用以下帖子中提到的任何技术: 如何格式化 JavaScript 日期

回答by Mahi M.

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

这将给出如下输出:10/27/2014, 12:58:45 PM

回答by Finny Abraham

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.

用于将 UNIX 时间戳转换为给定格式dd-mm-yyyy at hh:mm am;您必须首先构造一个 JavaScript Date 对象,然后使用原生 JavaScript Date 方法或 date.format jQuery 库。

Step 1: Constructing Date Object

第 1 步:构造日期对象

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

第 2 步:转换为给定格式

Option #1 - Using native JavaScript Date Methods

选项 #1 - 使用原生 JavaScript 日期方法

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.

注意:显示的日期将基于客户端时区。如果您需要基于 UTC 的时间;使用JavaScript 日期对象的getUTCDate()getUTCMonth()getUTCFullYear()getUTCHours()getUTCMinutes()getUTCHours()方法而不是提供的方法。

Option #2 - Using date.format jQuery Library

选项 #2 - 使用 date.format jQuery 库

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

  • 下载 date.format.js

    https://raw.githubusercontent.com/jacwright/date.format/master/date.format.js

  • 在 HTML 文件中包含 date.format.js

    <script src="/script dir/date.format.js"></script> //substitute "script dir" with the directory where date.format.js reside

  • 格式化日期对象

    您可以像在 PHP 中一样使用格式字符串

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

Examples

例子

Option #1

选项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

选项#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.

注意:还有其他库,比如 moment.js,它们也有类似的功能。