momentjs seconds until a date - javascript

I'm Using momentjs to find the time until a date.
moment(end).locale('nl').fromNow('s')
Currently using this, only this this returns 1 day
I'm looking for something like 86400
Is this possible with momentjs?

as of October 2018 , you can also do this :
const date = '20181031'
const time = '16:25:26'
console.log(moment().diff('2018/10/31 18:14:00')) // time difference in milliseconds

I manage to fix it using the following line of code
moment().diff(end, "seconds")
Note: this will give a negative time

Related

Format amount of seconds to minutes and seconds with momentjs

I'd like to use momentjs to use a given amount of seconds (e.g. 65) and format that to minutes and seconds (so in the case of 65 seconds it should format it to '01:05'). I've looked around the momentjs docs and also here but I couldn't quite get to the right solution. I know there's a way to do this with just pure JavaScript, but I'd like to use momentjs for this.
Any suggestions?
If you insist on doing this using moment.js, you can do something like this:
Start with a 00:00:00.000 time and add seconds, minutes, etc. to it.
Here is an example:
const result = moment("00:00:00.000", "hh:mm:ss.SSS").add(65, "s").format("mm:ss");
console.log(result);
<script src="https://cdn.jsdelivr.net/momentjs/2.14.1/moment-with-locales.min.js"></script>
The problem is that since this is the time of a date, when you add 24 hours to it, you'll end up with 00:00:00 instead of 24:00:00.

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

Calculate duration between momentjs timestamps in UTC

I am having a hard time to calculate the duration (difference in time) of two timestamps.
There is a timestamp I receive from the server in the following format:
start # => "2017-05-31 06:30:10 UTC"
(This is basically rubys DateTime.now.utc)
I want to see how many hours have passed since then until as of right now.
The calculation happens in the angularjs frontend only.
I tried the following:
var start = moment("2017-05-31 06:30:10 UTC", "YYYY-MM-DD HH:mm:ss Z").utc();
var now = moment.utc();
var duration = moment.duration(now.diff(start));
console.log(duration.asHours()); #=> 2 hours even though the point in time was just a couple of minutes ago.
Unfortunately this would always use my devices local time and produce a time that's a few hours off the actual time.
So my approach was to either convert all times to UTC and let momentjs handle all this.
What am I missing?
Since your input is UTC you can use moment.utc method.
By default, moment parses and displays in local time.
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().
Here a live example:
var start = moment.utc("2017-05-31 06:30:10 UTC", "YYYY-MM-DD HH:mm:ss Z");
var now = moment.utc();
var duration = moment.duration(now.diff(start));
console.log(duration.asHours());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
In your code sample, you are parsing the input string as local date and then converting it to UTC (with utc() method).
See Local vs UTC vs Offset guide to get more info.

option to display the time in PST by converting milliseconds

i am working on javascript. and i have time in milliseconds. so i have to display the time in PST time Zone. i have tried for GMT. but is there any option to display the time in PST format in which the input is in milliseconds like 1671673550214. so iam trying to convert this milliseconds to time and date format of PST. same as the below code does. pleas help.
Here is my tried code:
var time = new Date().getTime();
var date = new Date(time);
document.write(date.toString());
i have even tried of using UTCString(), UTC() and toString(); but things is getting converted to GMT and not to PST.
Please check this library, it has lot of functions to display and manupilate date time.
http://momentjs.com/
http://momentjs.com/docs/
Also please check following post
Moment.js: Format date in a specific timezone\
If you dont want to use external lib, see the following article
http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329
You can use timezonjs which help you to convert the time to any timezone.
https://github.com/mde/timezone-js

How to get difference of saved time and current time in jquery?

I want to get the time difference between saved time and current time in javascript or jquery. My saved time looks like Sun Oct 24 15:55:56 GMT+05:30 2010.
The date format code in java looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
How to compare it with the current time and get the difference?
Is there any inbuilt function in jquery or javascript??
Any suggestions or links would be appreciative!!!
Thanks in Advance!
Update
Date is stored as varchar in the DB. I am retriving it to a String variable and then change it to java.util.Date object. The java code looks like
String newDate = "2010/10/24 15:55:56";
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = format.parse(newDate);
This date object was sent to client. There i want to compare the saved date with current date and want to show the time difference like 2 secs ago, 2 hours ago, 2 days ago etc... like exactly in facebook. I have gone through some date to timestamp conversion tutorial in java script and now i can get the difference in timestamp. Now, i want to know how i shall change it to some format like "2 secs or 2 days or 24 hours"??. Or, how i shall change it back to date format???
Convert them into timestamps which are actually integers and can get subtracted from each other. The you just have to convert back the resulting timestamp to a javascript date object.
var diff = new Date();
diff.setTime( time2.getTime()-time1.getTime() );
You dont need to explicit convert, just do this:
var timediff = new Date() - savedTime;
This will return the difference in milliseconds.
jQuery doesn't add anything for working with dates. I'd recommend using Datejs in the event that the standard JavaScript Date API isn't sufficient.
Perhaps you could clarify exactly what input and output you're aiming for. What do you mean by "the difference?" There is more than one way to express the difference between to instants in time (primarily units and output string formatting).
Edit: since you said you're working with jQuery, how about using CuteTime? (Demo page)

Categories

Resources