Javascript 相对时间 24 小时前等作为时间

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

Javascript relative time 24 hours ago etc as time

javascriptdatetimehighcharts

提问by Totoro

I am trying to use highcharts to show some data over the last 24 hours. The chart requires the start time when you use time for the x axis like in this example Highcharts time example. I cant figure out how to tell it to start 24 hours ago for example, if the time now was 22:34pm on the 18th, I want it to start at 22:34pm on the 17th. I am not very good with time and date related code and Javascript is also not my strong point. I belive I would need the finished output to be something like: pointStart: Date.UTC(2012, 5, 17, 22, 34)For the above example, but I'm not so sure how to get that from Date().

我正在尝试使用 highcharts 显示过去 24 小时内的一些数据。当您使用时间作为 x 轴时,图表需要开始时间,如本例Highcharts 时间示例。我不知道如何告诉它在 24 小时前开始,例如,如果现在的时间是 18 日下午 22:34,我希望它在 17 日下午 22:34 开始。我不太擅长与时间和日期相关的代码,Javascript 也不是我的强项。我相信我需要完成的输出是这样的: pointStart: Date.UTC(2012, 5, 17, 22, 34)对于上面的例子,但我不太确定如何从 Date() 获取它。

Edit: I am not sure why it was marked as a duplicate but I was trying to get a time relative to the current time (now - 24h), not a relative string representation (“twenty four hours ago”). The other question also does not mention highcharts at all.

编辑:我不确定为什么它被标记为重复,但我试图获取相对于当前时间(现在 - 24 小时)的时间,而不是相对字符串表示(“二十四小时前”)。另一个问题也根本没有提到 highcharts。

回答by Naftuli Kay

This is actually fairly simple:

这实际上相当简单:

var yesterday = new Date(new Date().getTime() - (24 * 60 * 60 * 1000));

Simply construct a new Datewith the value of the current timestamp minus 24 hours.

只需Date使用当前时间戳减去 24 小时的值构造一个新的。

(24 hours multiplied by 60 minutes in each hour multiplied by 60 seconds in each minute multiplied by 1000 milliseconds in each second)

(24 小时乘以每小时 60 分钟乘以每分钟 60 秒乘以每秒 1000 毫秒)

回答by Julian Hollmann

You should use timestamps as you can calculate with them.

您应该使用时间戳,因为您可以使用它们进行计算。

This is how you get the current timestamp: Math.round(new Date().getTime() / 1000)Please note that this the computers local time.

这是您获取当前时间戳的方式:Math.round(new Date().getTime() / 1000)请注意,这是计算机本地时间。

Now you can get the timestamp 24 hours ago like this:

现在你可以像这样获得 24 小时前的时间戳:

var ts = Math.round(new Date().getTime() / 1000);
var tsYesterday = ts - (24 * 3600);

Please see this fiddle: http://jsfiddle.net/Mjm7V/

请看这个小提琴:http: //jsfiddle.net/Mjm7V/

Edit:As Nick correctly pointed out, Date#getTimereturns the UTC timestamp (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime)

编辑:正如尼克正确指出的那样,Date#getTime返回 UTC 时间戳(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime

回答by chovy

24 hours ago:

24小时前:

new Date(Date.now() - 86400 * 1000).toISOString()

new Date(Date.now() - 86400 * 1000).toISOString()

  1. now: new Date().toISOString()
  2. outputs: '2017-02-04T09:15:25.233Z'
  3. Date.now()returns seconds since epoch.
  4. Subtract 86400seconds in a day times 1000to convert to milliseconds
  5. outputs: '2017-02-03T09:14:11.789Z'
  1. 现在: new Date().toISOString()
  2. 输出: '2017-02-04T09:15:25.233Z'
  3. Date.now()返回自纪元以来的秒数。
  4. 减去86400一天中的秒数1000以转换为毫秒
  5. 输出: '2017-02-03T09:14:11.789Z'

回答by bjornd

Just subtract the amount of milliseconds in 24 hours from the date:

只需从日期中减去 24 小时内的毫秒数:

new Date (Date.UTC(2012, 5, 17, 22, 34) - 24 * 3600 * 1000)