Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript relative time 24 hours ago etc as time [duplicate]

I am trying to use highcharts to show some data over the last 24 hours. The chart requires the start time when you use the time for the x axis like in this example Highcharts time example. I can't 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 believe 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().

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.

like image 234
Totoro Avatar asked Jun 17 '12 15:06

Totoro


People also ask

How do you display JavaScript datetime in 24 hour format?

Use the toLocaleString() method to change time formatting to 24 hours, e.g. date. toLocaleString('en-US', {hour12: false}) . The toLocaleString method returns a string that represents the date and time according to the provided locale and options parameters.

What is new Date () getTime ()?

The date. getTime() method is used to return the number of milliseconds since 1 January 1970. when a new Date object is created it stores the date and time data when it is created. When the getTime() method is called on this date object it returns the number of milliseconds since 1 January 1970 (Unix Epoch).

What is TimeSpan in JavaScript?

TimeSpan(millis : int) Creates a TimeSpan from the specified amount of milliseconds. TimeSpan(days : int, hours : int) Creates a TimeSpan from the specified amount of days and hours. TimeSpan( hours : int, minutes : int, seconds : int)

How to display time in relative format in JavaScript?

Showing relative time such as "1 day ago", "5 days ago" etc previously required external Javascript libraries such as Moment.js. But now browsers have added native support for this — you may not need external libraries. The Intl.RelativeTimeFormat object can be used to display time in relative format.

How long ago is a day ago in JavaScript?

The former would say things like 24 hours ago / 365 days ago. The latter (what is reflected in the edit) says a day ago, / a year ago For Moment.js users, it has fromNow () function that returns "x days" or "x hours ago" from current date/time.

Is there a way to format a date/time object with relativetimeformat?

The Intl.RelativeTimeFormat API seems more of a locale conscious string formatter (for time units) than something for formatting raw date/time objects. You still have to process the timestamp to decide the appropriate time frame (minute, hour, days etc) to use. You don't want to be saying 36,020 seconds ago!

Is there a way to convert a timestamp to relative time?

This, as an answer, is incomplete without going from a timestamp to relative time. The Intl.RelativeTimeFormat API seems more of a locale conscious string formatter (for time units) than something for formatting raw date/time objects. You still have to process the timestamp to decide the appropriate time frame (minute, hour, days etc) to use.


2 Answers

This is actually fairly simple:

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

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

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

like image 150
Naftuli Kay Avatar answered Sep 24 '22 15:09

Naftuli Kay


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.

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

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

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

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

like image 39
Julian Hollmann Avatar answered Sep 22 '22 15:09

Julian Hollmann