Javascript jQuery 格式时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8801254/
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-24 07:33:53  来源:igfitidea点击:

jQuery Format Time

javascriptjquery

提问by DiegoP.

I have a jQuery script that receives a string in milliseconds inside a parameter, like this:

我有一个 jQuery 脚本,它在参数中以毫秒为单位接收一个字符串,如下所示:

params.tweetDate='77771564221';

What I need to do is to create a jQuery function that will be able to format this milliseconds string in a USA time, like 10.00 AMor 10.00 PM.

我需要做的是创建一个 jQuery 函数,它能够在美国时间格式化这个毫秒字符串,比如10.00 AM10.00 PM

Is there a jQuery function that is able to do this?

是否有能够做到这一点的 jQuery 函数?

Please help.

请帮忙。

Thanks

谢谢

回答by YuS

There is Date object in pure javascript, no jQuery needed. http://www.javascriptkit.com/jsref/date.shtml

纯 javascript 中有 Date 对象,不需要 jQuery。 http://www.javascriptkit.com/jsref/date.shtml

Example:

例子:

var time = new Date(params.tweetDate),
h = time.getHours(), // 0-24 format
m = time.getMinutes();
// next just convert to AM/PM format (check if h > 12)

回答by T.J. Crowder

No, there's no jQuery function for this. You can use

不,没有用于此的 jQuery 函数。您可以使用

  • JavaScript's own Dateobject, using the getHours()and getMinutes()functions, handling the AM/PM thing yourself (e.g., hours >= 12 is PM), padding out the minutes with a leading 0 if minutes is less than 10, etc. Also note that if hours is 0, you want to make it 12 (because when using the AM/PM style, you write midnight as "12:00 AM", not "0:00 AM").
  • DateJS, an add-on library that does a huge amount of date stuff (although sadly it's not actively maintained)
  • PrettyDatefrom John Resig (the creator of jQuery)
  • JavaScript 自己的Date对象,使用getHours()getMinutes()函数,自己处理上午/下午的事情(例如,小时 >= 12 是下午),如果分钟小于 10,则用前导 0 填充分钟等。还要注意,如果小时是0,您想将其设为 12(因为在使用 AM/PM 样式时,您将午夜写为“12:00 AM”,而不是“0:00 AM”)。
  • DateJS,一个附加库,可以处理大量的日期内容(虽然遗憾的是它没有得到积极维护)
  • 来自 John Resig(jQuery 的创建者)的PrettyDate

To use just about any of those, first you have to turn that "milliseconds" value into a Dateobject. If it's really a "milliseconds" value, then first you parse the string into a number via parseInt(str, 10)and then use new Date(num)to create the Dateobject representing that point in time. So:

要使用其中的任何一个,首先必须将该“毫秒”值转换为一个Date对象。如果它确实是一个“毫秒”值,那么首先将字符串解析为一个数字parseInt(str, 10),然后使用它new Date(num)来创建Date表示该时间点的对象。所以:

var dt = new Date (parseInt(params.tweetDate, 10));

However, the value you've quoted, which you said is a milliseconds value, seems a bit odd — normally it's milliseconds since The Epoch (Jan 1, 1970), which is what JavaScript uses, but new Date(parseInt("77771564221", 10))gives us a date in June 1972, long before Twitter. It's not secondssince The Epoch either (a fairly common Unix convention), because new Date(parseInt("77771564221", 10) * 1000)gives us a date in June 4434. So the first thing to find out is what that value actually represents, milliseconds since when. Then adjust it so it's milliseconds since The Epoch, and feed it into new Date()to get the object.

但是,您引用的值(您所说的毫秒值)似乎有点奇怪——通常是自 The Epoch(1970 年 1 月 1 日)以来的毫秒数,这是 JavaScript 使用的,但new Date(parseInt("77771564221", 10))给了我们 1972 年 6 月的日期,早在推特之前。从 The Epoch 开始也不是(一个相当常见的 Unix 约定),因为new Date(parseInt("77771564221", 10) * 1000)它给了我们一个 4434 年 6 月的日期。所以首先要找出这个值实际上代表什么,从when算起的毫秒数。然后将其调整为自 The Epoch 以来的毫秒数,并将其输入new Date()以获取对象。

回答by tika

Here is a function for you:

这里有一个功能给你:

function timeFormatter(dateTime){
var date = new Date(dateTime);
if (date.getHours()>=12){
    var hour = parseInt(date.getHours()) - 12;
    var amPm = "PM";
} else {
    var hour = date.getHours(); 
    var amPm = "AM";
}
var time = hour + ":" + date.getMinutes() + " " + amPm;
console.log(time);
return time;
}

You may call the function in any approach like:

您可以使用任何方法调用该函数,例如:

var time = timeFormatter(parseInt("2345678998765"));

回答by badMonkey

Using T.J.'s solution this is what I ended up with.

使用 TJ 的解决方案,这就是我的结果。

var date = new Date(parseInt("77771564221", 10));
var result = new Array();
result[0] = $.datepicker.formatDate('DD, M, d, yy', date);
result[1] = ' ';
if (date.getHours() > 12) {
    result[2] = date.getHours() - 12;
} else if (date.getHours() == 0 ) {
    result[2] = "12";
} else {
    result[2] = date.getHours();
}
result[3] = ":"
result[4] = date.getMinutes();

if (date.getHours() > 12) {
    result[5] = " pm";
} else {
    result[5] = " am";
}

console.log(result.join(''));

回答by Orentet

take a look at timeago: this is a jquery plugin used exactly for this purposes.

看看timeago:这是一个专门用于此目的的 jquery 插件。