I receive two types of date, seconds and milliseconds for which i am trying to convert to a date.
Seconds: 1458820878062
Milliseconds: 1458823059491000
I can convert seconds via:
function convertToDate() {
var date = 1458820878062;
var m = moment.utc(date).utcOffset(moment().format('ZZ'));
return m.format('DD-MM-YY HH:mm:ss.SSS');
}
How can i convert milliseconds in to a date? And apply the necessary conversion to whatever date may be?
Something along the lines of:
if(date.length = 13) {
// Do seconds conversion
if(date.length > 13) {
// Do milliseconds conversion
You can try this:
moment("/Date(1458823059491000/1000)/")
ie., you need to divide the milliseconds by 1000 to get the time in seconds and then you can use it.
Moment supports both operations out of the box.
If you have seconds, use moment.unix:
moment.unix(1458855925).format()
"2016-03-24T16:45:25-05:00"
If you have milliseconds, just use the moment constructor directly:
moment(1458856019742).format()
"2016-03-24T16:46:59-05:00"
To get the unix seconds from a moment, use:
moment().unix()
1458856086
To get the unix milliseconds from a moment, use:
moment().valueOf()
1458856019742
How about just check if(data > 1e13) ?
Related
I need to get the difference (in minutes) from a datetime that I get froma get request in a string format to now.
According to my research, I can use moment.js to do so, but I haven't figured out now.
That format I am getting the date/time to be compared is as:
2017-02-10T20:52:13.885Z
I have already tried to do some operations with moment.js such as
moment().startof(comparedTime).fromNow())
But it returns nothing.
What are the alternatives and the best way to do this?
Can't you just use vanilla javaScript?
var getDate = '2017-02-10T20:52:13.885Z'; //get time from server
var parseDate = new Date(getDate).getTime(); //change string into Date object into milliseconds
var nowDate = Date.now(); //get current Date in milliseconds
var minutes = Math.round((nowDate-parseDate)/1000/60); //subtract times, count seconds (/1000), count minutes (/60)
console.log(minutes);
You need to create a moment object by passing the date string in. e.g.
myDate = moment(myISOString)
https://momentjs.com/docs/#/parsing/
Then you can use the moment object as described in the docs.
With Moment.js, this is simply:
moment().diff('2017-02-10T20:52:13.885Z', 'minutes') // 65
If you want partial minutes included, then pass true as a third parameter:
moment().diff('2017-02-10T20:52:13.885Z', 'minutes', true) // 65.04565
var timeArr = moment().format('HH:mm:ss').split(':');
var timeInMilliseconds = (timeArr[0] * 3600000) + (timeArr[1] * 60000);
This solution works, test it, but I'd rather just use the moment api instead of using my own code.
This code returns TODAYS time in milliseconds. I need it to call another function in milliseconds...Can not use the epoch. Need today's time formated in milliseconds. 9:00am = 3.24e+7 milliseconds 9:00pm = 6.84e+7 milliseconds.
From the docs:
http://momentjs.com/docs/#/parsing/unix-timestamp-milliseconds/
So use either of these:
moment(...).valueOf()
to parse a preexisting date and convert the representation to a unix timestamp
moment().valueOf()
for the current unix timestamp
See this link http://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/
valueOf() is the function you're looking for.
Editing my answer (OP wants milliseconds of today, not since epoch)
You want the milliseconds() function OR you could go the route of moment().valueOf()
var timeArr = moment().format('x');
returns the Unix Millisecond Timestamp as per the format() documentation.
You could subtract the current time stamp from 12 AM of the same day.
Using current timestamp:
moment().valueOf() - moment().startOf('day').valueOf()
Using arbitrary day:
moment(someDate).valueOf() - moment(someDate).startOf('day').valueOf()
You can just get the individual time components and calculate the total. You seem to be expecting Moment to already have this feature neatly packaged up for you, but it doesn't. I doubt it's something that people have a need for very often.
Example:
var m = moment();
var ms = m.milliseconds() + 1000 * (m.seconds() + 60 * (m.minutes() + 60 * m.hours()));
console.log(ms);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Since this thread is the first one from Google I found, one accurate and lazy way I found is :
const momentObject = moment().toObject();
// date doesn't exist with duration, but day does so use it instead
// -1 because moment start from date 1, but a duration start from 0
const durationCompatibleObject = { ... momentObject, day: momentObject.date - 1 };
delete durationCompatibleObject.date;
const yourDuration = moment.duration(durationCompatibleObject);
// yourDuration.asMilliseconds()
now just add some prototypes (such as toDuration()) / .asMilliseconds() into moment and you can easily switch to milliseconds() or whatever !
I want to get seconds remaining between a current dataTime and future dateTime.
I am using Meteor + MongoDb.
In Mongo DataTime is saved like this:
2015-12-11T06:14:39.671Z
I want seconds remaining between current datatime and future or expiry datetime.
keep your time data in epoch format which is basically storing data in milliseconds. then you'll be easy able to compare using momentJS or substracting. And in MongoDB , this data will be stored as Number type not Date
var time= (new Date).getTime();
this piece of code will return time in milliseconds.
The javascript Date can handle the sample input as listed in your question. If that format is consistant, the following should get you your answer:
var difference = (new Date(databaseDate).getTime()-Date.now())/1000;
Where Date.getTime() and Date.now() are in milliseconds (hence the 1000).
So, I need to convert this kind of Date in ISO format:
"05-Mar-13 17:00:00.000000"
But when I do something like this:
var Time = (new Date("05-Mar-13 17:00:00.000000")).toISOString().replace('Z', Milliseconds);
I've got in variable Time another hour:
"2013-03-05T16:00:00.000000"
So It changes on another hour.
What should I do to avoid this changing of hours?
The ISO format is supposed to convert the time to UTC.
Your browser supposes that the time you are passing the Date constructor is based on your local time, supposedly one hour behind UTC.
To counter this, you can use
new Date().getTimezoneOffset();
which will return the time offset in minutes. In your case it will return -60.
A complete example:
function getTime() {
var date = new Date("05-Mar-13 17:00:00.000000")
date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
return date.toISOString();
}
Note that this can set time minutes to a negative value, but the Date object is smart enough to convert that to a new time with positive minutes again by changing the hour.
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".
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?
10/10/13
10.10.13
10th October 2013
101013
I would look at this post: https://stackoverflow.com/a/3177838/2895307
In it he just uses a javascript Date() as the parameter to the "timeSince()" function. To create a javascript Date from your hardcoded string you can use this format:
var d1 = new Date("October 13, 1975 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.
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
Math.round(Date.now() - anHourAgo) / 1000
Finally here's a working example of all that code in action:
http://codepen.io/DavidVValdez/pen/axHGj
i hope this helps, cheers!
The easiest thing to do would be to use Date.getTime().
This will give you the number of milliseconds since the Unix epoch and will make the math very simple.
Date.getTime