简体   繁体   English

使用jquery将确切的日期转换为时间 - 日期的格式是什么?

[英]Convert exact date to time ago using jquery - What format for the date?

I have a static page which will specify a hardcoded exact date. 我有一个静态页面,它将指定一个硬编码的确切日期。 If the use has javascript, I want to then convert this hardcoded exact date into a "time ago". 如果使用javascript,我想将此硬编码的确切日期转换为“time ago”。

For example: 例如:

3 hours ago

My question is, in what format of date will javascript be able to most efficiently convert to the time ago? 我的问题是,以什么格式的日期将javascript能够最有效地转换到前一段时间?

  • 10/10/13 13年10月10日
  • 10.10.13 10.10.13
  • 10th October 2013 2013年10月10日
  • 101013 101013

I would look at this post: https://stackoverflow.com/a/3177838/2895307 我会看看这篇文章: https//stackoverflow.com/a/3177838/2895307

In it he just uses a javascript Date() as the parameter to the "timeSince()" function. 在其中,他只使用javascript Date()作为“timeSince()”函数的参数。 To create a javascript Date from your hardcoded string you can use this format: 要从硬编码字符串创建javascript日期,您可以使用以下格式:

var d1 = new Date("October 13, 1975 11:13:00") var d1 = new Date(“1975年10月13日11:13:00”)

definitely unix timestamp is the best format for all date and time calculations, you can convert the results to a more readable format later. 绝对unix时间戳是所有日期和时间计算的最佳格式,您可以稍后将结果转换为更易读的格式。

the calculation is simple, you start with the timestamp of an event in the past, for example: 计算很简单,您可以从过去事件的时间戳开始,例如:

var anHourAgo = Date.now() - 3600000;

then you substract that from the current timestamp and get the number of milliseconds that have passed since that event 然后从当前时间戳中减去该值,并获得自该事件以来经过的毫秒数

Date.now() - anHourAgo

then you can pass that to any function that will convert those milliseconds to hours, minutes and seconds, here's an example that takes seconds and returns an array with that info, and another function that pads those numbers with zeros 然后你可以将它传递给任何将这些毫秒转换为小时,分钟和秒的函数,这是一个需要几秒钟的例子并返回一个包含该信息的数组,另一个函数用零填充这些数字

var zeroPad = function(n){
    return n.toString().replace(/^(\d)$/,'0$1');
};

var formatSecs = function(s){
    var r = [
        Math.floor(s / 3600),
        Math.floor(s%3600 / 60),
        Math.floor((s%3600)%60)
    ];
    r.push(zeroPad(r[0])+':'+zeroPad(r[1])+':'+zeroPad(r[2]));
    return r;
};

the formatSecs function expects seconds instead of millseconds, you should divide by 1000 and round that number, then pass that number to the function formatSecs函数需要秒而不是毫秒,你应该除以1000并舍入该数字,然后将该数字传递给函数

Math.round(Date.now() - anHourAgo) / 1000

Finally here's a working example of all that code in action: 最后,这是所有代码的实际运行示例:

http://codepen.io/DavidVValdez/pen/axHGj http://codepen.io/DavidVValdez/pen/axHGj

i hope this helps, cheers! 我希望这会有所帮助,欢呼!

The easiest thing to do would be to use Date.getTime() . 最简单的方法是使用Date.getTime()

This will give you the number of milliseconds since the Unix epoch and will make the math very simple. 这将为您提供自Unix时代以来的毫秒数,并使数学变得非常简单。

Date.getTime Date.getTime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM