Could someone explain to me what this returning number means? and how it is derived to that?
console.log(Date.now() - 24 * 60 * 60 * 1000);
If I wanted to use the above formula to display the next 15minutes and not 24 hours? how would I alter it?
Date.now() returns:
the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.
24 * 60 * 60 * 1000 in milliseconds represents 24 hours*. So you basically get a timestamp 24 hours in the past from now. Notice that due to DST this doesn't necessarily compute a timestamp one day in the past. It's 24 hours in the past.
Also to get some meaningful output you should wrap resulting number in Date:
console.log(new Date(Date.now() - 24 * 60 * 60 * 1000));
Finally Date.now() can be replaced with new Date() when using in arithmetic expression.
* - 24 (hours) times 60 (minutes in hour) times 60 (seconds in minute) times 1000 milliseconds in second.
Related
I have a Webhook listener that receives a unix timestamp value.
This timestamp is my END time.
I would like to use the current unix timestamp and compare what HH:MM:SS are left until the end time.
I was reading this post: How can i find HH:MM:SS difference between two UNIX timestamps?
and think it is very similar to my needs but needs a little tweaking.
Example:
Current time = unix now time
End time = unix time
= How many HH:MM:SS remain before the time has ended
I was trying;
function timeDiff(EpochTime) {
let msec = (new Date()).valueOf() - EpochTime * 1000;
const hh = Math.floor(msec / 1000 / 60 / 60);
msec -= hh * 1000 * 60 * 60;
const mm = Math.floor(msec / 1000 / 60);
msec -= mm * 1000 * 60;
const ss = Math.floor(msec / 1000);
msec -= ss * 1000;
return `${mm}m ${ss}s`;
}
Thanks
Magik
GNU awk is an option:
awk -v etim="2021 02 08 16 27 00" '{ print strftime("%c",mktime(etim)-strftime("%s"),1) }' <<< /dev/null
Pass the date/time as etim and the use GNU awk's strftime and mktime functions to print the difference in the passed date and now in the locale format. Change %c to what ever format is required.
I have created a date object for Jan 1 1970 and increasing 60 minutes through iteration.
d = new Date(1970, 0, 1);
loop Start
d.setTime(d.getTime() + (60 * 60 * 1000) );
loop End
while date object reaches 10 AM and adding 60 minutes to that also return the same time(10 AM).
This problem occurred only with fire fox browser.
that too only my machine timezone is Australia/Melbourne.
I have a piece of code like this:
window.setInterval("reloadIFrame();", 3000);
^
*
I want to know if there is a chart anywhere that can translate js time (*) to real hours, like one hour 2 hour three hour is there any way?
That 3000 is just expressed in milliseconds, so standard math will do.
3000ms = 3000ms / 1000ms/s / 3600s/h = .00083 hours
The parameter does not accept hours, you would have to multiply to get it in millisecond.
1000 ms = 1 second
60 seconds = 1 minute
60 minutes = 1 hour
2 hours -> 60 min * 2 hours = 120 minutes * 60 = 7,200 seconds * 1000 = 7,200,000 ms
The reverse would be a division.
3000 ms / 1000 = 3 seconds / 60 = 0.05 minute / 60 = 0.00083 hours
Is there a built in method in extjs or javascript for converting milliseconds to a time?
I found one for date, but it doesn't work. I always get Jan, 1 1970 08:00 (Pacific Standard Time). When I try test.getHours I get 0. I am trying to print out 8:00 or 08:00
var getSignOnRecord = 28800000;
var test = new Date(getSignOnRecord);
test.getHours() // 0 ???? Should be 8
you can use ISOString date formats for up to 24 hours of time:
new Date(28800000).toISOString().split("T")[1].split(".")[0]; // == "08:00:00"
you can easily slice() the remaining text to eliminate seconds or whatnot.
this works because using a "unix" stamp results in an GMT offset, and ISO also displays GMT, so by throwing away the date part, you're left with a pretty readable format of up to 23h59m59s...
You are getting the localized hour, but you want the hours at UTC
new Date(28800000).getUTCHours() // 8
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours
The getHours() method returns the hour for the specified date, according to local time.
new Date(value);
value: Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC
28800000 ms is indeed 1 January 1970, 8h
When I try test.getHours I get 0
NB value is UTC, getHours is local time
This is a math problem. As far as I know, there is no function that does this in native JavaScript, but can be coded from scratch.
function convertToTime(milliseconds) {
var seconds = Math.floor(milliseconds / 1000) % 60
var minutes = Math.floor(milliseconds / (1000 * 60)) % 60
var hours = Math.floor(milliseconds / (1000 * 60 * 60)) % 24
return (hours < 10 ? "0" + hours : hours) + ":" +
(minutes < 10 ? "0" + minutes : minutes) + ":" +
(seconds < 10 ? "0" + seconds :seconds);
}
There's also probably a method like this in momentjs.
How to Expire a Cookie in 30 min ? I am using a jQuery cookie.
I am able to do something like this.
$.cookie("example", "foo", { expires: 1 });
This is for 1 day. But how can we set expiry time to 30 min.
30 minutes is 30 * 60 * 1000 miliseconds. Add that to the current date to specify an expiration date 30 minutes in the future.
var date = new Date();
var minutes = 30;
date.setTime(date.getTime() + (minutes * 60 * 1000));
$.cookie("example", "foo", { expires: date });
If you're using jQuery Cookie (https://plugins.jquery.com/cookie/), you can use decimal point or fractions.
As one day is 1, one minute would be 1 / 1440 (there's 1440 minutes in a day).
So 30 minutes is 30 / 1440 = 0.02083333.
Final code:
$.cookie("example", "foo", { expires: 30 / 1440, path: '/' });
I've added path: '/' so that you don't forget that the cookie is set on the current path. If you're on /my-directory/ the cookie is only set for this very directory.
I had issues getting the above code to work within cookie.js. The following code managed to create the correct timestamp for the cookie expiration in my instance.
var inFifteenMinutes = new Date(new Date().getTime() + 15 * 60 * 1000);
This was from the FAQs for Cookie.js