Javascript Date getTime() code snippet with mysterious additional characters - javascript

<script language="JavaScript">
var t = new Date();
t.getTime() + -864e5;
</script>
What is that funky code after the "+" at the end of the second line doing?
It is probably made to be hard to understand, since I suspect it's one of the ways they try to protect themselves against scraping.

It is a valid JavaScript number that represents the number of milliseconds in a 24 hour day.
1000*60*60*24 or 86400000 or 864e5

-864e5 means "minus 1 day". So the JavaScript is really getting the date/time 24 hours ago.

864e5 is a valid JavaScript number that represents the number of milliseconds (a millisecond is 1/1000'th of a second) in a 24 hour day.
1000*60*60*24 = 86400000 or using exponential notation 864e5

It looks like the + -864e5 is offsetting the time 1 day into the past.
Its true its not very readable, or makes much sense to people looking at it for a first time, but there isn't really any other way in bare js (at this point).

Related

Javascript format special time

I have from a software a output like this timevalue:
15606260000 s
I don't know what format this is. The value in minutes is approximately 30 minute
How can I convert this with javascript in hour:min:seconds?
I think this is a epoch time or so?
this looks like a unix timestamp value (count in seconds since 01/01/1970)
what you can do in javascript is:
let d = new Date(15606260000); console.log(d.getUTCHours() + ":"+ d.getUTCMinutes() +":"+ d.getUTCSeconds());
and you can take it from there
You can use this link for a proper documentation
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
keep in mind that JS time is in milliseconds most of the times you need to multiply by 1000

How to work with Moment.js to return time in a 24-hours format when adding to it?

I get hours as strings in my app, e.g. "2230". I would like to be able to add minutes to it, so as to simulate the time that it will be after those minutes have been added to it, e.g.
//"2230" + "60"mins = "23:30"
//"2230" + "180"mins = "02:30"
I read that Moment.js could be a solution to this, but I couldn't figure out:
what the right way to format the hours initially is with moment("2230").format()
how to add minutes to it
how to make it behave like a 24-hour clock when adding to it
Moment is a great tool for doing this. It takes some syntax tricks to get it right, but I think this is what you're looking for:
moment("2230", "HH:mm")
.add(60, "minutes")
.format("HH:mm")
Feel free to play around with it here:
https://codesandbox.io/s/proud-pine-lz0fs?file=/src/index.js
As you can see, as long as your time string is always 4 characters long, moment can extract the HH:mm format, then you can add minutes, and then format the final output to HH:mm again.
Here are 2 great resources:
https://techstream.org/Bits/Javascript/Javascript-Parse-time
https://flaviocopes.com/momentjs/
Hope that helps!
First you have to split this string to get the hours and minutes from it.
const s= "2230"
const hour = s.substring(0,2);
const min = s.substring(2,4);
After that you can easily pass this hours and min to a moment.
const time = moment()
.set({"hour": hour, "minute": min})
.add(60, 'minutes')
.format("HH:mm");
the .set is to set the time (hours minutes)
the .add is to add the minutes you wanted to add
the .format is to format the output as you pleased,
NOTE the capital "HH" means 24/h clock while "hh" means 12/h clock

Using moment.js how do I give a specific format for a duration

Given a duration of a number of seconds coming from an API as duration_seconds = 86485.
(1 day, 0 hours, 1 minute, 1 second)
I was going to use moment.js to convert this into formatted duration as follows:
1. {d} {hh}:{mm}:{ss} (1d 00:01:01)
2. {d}d {hh}h m{mm} s{ss} (1d 04h 30m 23s)
I would also like to ensure the following:
Days are trimmed if days is 0 (00:01:01 - for 1 minute 1 second)
hh:mm:ss are never trimmed, 1 second shows as 00:00:01.
I can create the duration like this:
moment.duration(duration_seconds, 'seconds')
The humanize function is not suitable as it approximates.
I can also write my own with:
duration.get('d')
duration.get('h')
duration.get('m')
duration.get('s')
I can't seem to find a built in function but I assume this would be an obvious one? Is there something I am missing, otherwise I can PR one into moment library.
This seems to imply that the function still does not exist:
Using moment.js, How can I simplify a duration to its most simplified form?
The moment-duration-format plugin can assist you with this.
// your input
var duration_seconds = 86485;
// create a moment-duration object
var duration = moment.duration(duration_seconds, 'seconds');
// format the object with a string
var formatted = duration.format('h[h] m[m] s[s]');
// displaying the output here
document.getElementById('output').innerHTML = formatted;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-duration-format/1.3.0/moment-duration-format.min.js"></script>
<div id="output" />
Note that by default it will omit units that are not relevant, so if your input is 3, it's just going to say "3s", not "0h 0m 3s".
If you want to change this behavior, set trim:false, per the documentation. For example, to get the format you mentioned in comments, use:
.format('hh:mm:ss', { trim: false })
Maybe you can try this?
moment(duration).format("DD MMM YYYY hh:mm: a")
Though you might have to convert your seconds to milliseconds. This can be done by saying duration * 1000.
Found from this answer: Moment js convert milliseconds into date and time

Subtract time in 24 hour format using javascript or jquery

I have a start and end time in the format of HH:MM and I need to get the total time in hours. I have tried a couple methods like trimming the string to an hour and minutes then multiplying the hours by 60 and adding the minutes but since the minute string is two digits, 00 I get an answer that is a power of ten greater than what I need. I also feel like this approach is inefficient.
So what is the best way to subtract 04:30 and 22:00? I also have access to moment.js if that is helpful.
Using moment.js' difference
var a = moment('22:00', 'HH:mm');
var b = moment('04:30', 'HH:mm');
a.diff(b, 'hours', true)

get time different (minutes) in javascript

How do I calculate the difference in minutes given two strings. For example say I have
11:00
11:30
But of course the second string could be 12:11 so I can't subtract just the minutes.
first use javascript to convert the strings to time, then subtract, then convert back to strings
like this:
x = new Date("1/1/01 11:00")
y = new Date("1/1/01 11:30")
// now y-x has difference in milliseconds
// (y-x)/1000 is difference in seconds, etc
The data 1/1/01 is just being used as a dummy value, but the one thing you might have to worry about is are the times on different days, if so you will have to use 1/2/01 for the second time. Unless of course you always know the times are in the same day, but if they can cross "midnight" then you have to adjust for that.
You may want to use http://momentjs.com/ which will take care of the details for you.
When looking for getting metrics such as date , hour , minutes, seconds from the date difference, it's a lot easier to use basic notations as listed here
var x = new Date(new Date().getTime() + 11.5*60*60000); // adds 11 hours - 30 minutes
var y = new Date(new Date().getTime() + 11*60*60000); // adds 11 hours
alert(x.getMinutes() - y.getMinutes()); // gives the difference = 30
Here's an example : https://jsfiddle.net/DinoMyte/157knmgn/

Categories

Resources