Moment display seconds as 'm:ss' - javascript

I am trying to use moment to display 2 seconds like this 0:02.
I expected the following code to work but unfortunately it just displays as0:00. This is based on this section of the docs.
moment(2).format('m:ss');
Can anyone advise where I am going wrong? I have read through the docs and tried numerous approaches with no luck.

You do it as
moment().minutes(0).second(2).format('m:ss')
https://jsfiddle.net/4eqL594p/
But this also sets minutes as 0.
EDIT: As another answerer had pointed out, you could also do
moment({seconds: 2}).format('m:ss')

Try Unix method, it accept an integer value representing the number of seconds since the Unix Epoch (Jan 1 1970 12AM UTC)
Example:
moment.unix(2).format('m:ss');
Demo:
console.log(moment.unix(2).format('m:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>

Related

moment.js always returns 0 for minutes()

I am trying to convert a moment into local time. However, when I try to get the minutes value from the moment it returns 0 even though it clearly is not 0 as can be seen from the picture the minutes is actually 35. Likewise, even if I do not try to get the minutes and format it to local time the minutes are again set to 0. In the example below 12:00 PM gets returned. What am I doing wrong and how do I correctly get the minutes portion of the moment or just as good how do I format it to local time with out the minutes being set to 0?
EDIT
As suggested in the comments formatting like console.log(m.local().format('hh:mm')) does not have any effect the min are still disregarded. See image.
Also, I'm using version "moment": "^2.29.3"

MomentJS is sometimes adding the wrong amount of hours

So what i'm simply trying to achieve a feature where i add X.X amount of hours to a 24h time. The issue is for the time 00:00, an incorrect amount of hours is added
The code i wrote works for the most part. It works for every possible time except 0:00.
If i have 01:30 and i add 1h it gives me 02:30. If I have 02:30 and i add 1.5h it gives me 04:00.
So heres this issue. When i have 00:00 and i add 1h i get 01:06...seems to make no sense and i was hoping someone with more momentJS experience might have some thoughts or ideas on the matter. Heres the code
If i have 00:00 and have 3.5 i get 03:06. This all only seems to happen when i start with 00:00. Kind of driving me crazy
console.log(timesheetRows[i][p]); //prints 0:00
console.log(Number(hours)); // prints 1
timesheetRows[i][p] = moment.utc(timesheetRows[i][p], 'hh:mm').add(Number(hours), 'hours').format('HH:MM');
You're using the wrong formats, hh stands for 12 hours and MM stands for two digits month number.
Use something like this:
moment.utc(timesheetRows[i][p], 'HH:mm').add(Number(hours), 'hours').format('HH:mm');

Yesterday, my Date.now() overflowed and I can't find any relevant results on Google

1423781658625
This number is the overflow. Converted to a date, this is:
Thu Feb 12 2015 22:54:18 GMT
Has anybody else encountered this issue yesterday?
I'm going to go out on a limb here... and answer the question that was asked.
Q: Has anybody else encountered this issue yesterday?
And by this issue, you are referring to some kind of "overflow" issue with Javascript Date.now().
I didn't encounter this issue. Nor would I expect to.
Javascript Date object supports values 100,000,000 days after 1/1/1970. And the now() method returns the value of object as the number of milliseconds since 1/1/1970.
There's 86,400 seconds in a day, times 1,000, gets milliseconds in a day, times 100,000,000 gives a maximum value of 8,640,000,000,000,000.
The maximum safe integer value in Javascript is Number.MAX_SAFE_INTEGER, which is equivalent to Math.pow(2, 53) - 1, a decimal value of 9,007,199,254,740,991.
The value returned by Date.now() isn't going to exceed MAX_SAFE_INTEGER, and even it did exceed that value, it wouldn't "overflow", it would just lose precision.
So, I'm going to go out on a limb, and answer your question you asked:
A: No. No one else encountered an overflow issue with Javascript Date.now() yesterday.
But I'll also follow that up with a suggestion that it's not Date.now() causing an overflow. If there's some type of integer overflow happening in your Javascript code, it's not an issue with Date.now(), but with something else you are doing. We could take guesses, but without additional information about what your code is actually doing (like the actual code), we'd just be guessing.
It turns out this is a result of an upper limit on the setTimer function. Thanks for all your replies!

Moment.js 2 different date strings when i parse using moment gives same value

I am parsing 2 different date strings
var d1 = '2014-02-01T00:00:00.000+0530'
var d2 = '2014-02-23T00:00:00.000+0530'
when i parse them using moment
alert(moment(d1, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
alert(moment(d2, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
both of them print Sat Feb 1 2014 xxxxx
what is wrong with it??
here is the link to the fiddle i created
jsfiddle
I think your moment formatting string is causing you the problem. If I remove this, the dates do not print as the same.
http://jsfiddle.net/K5ub8/7/
EDIT: The specific issue is you are using dd for day, instead of DD. http://momentjs.com/docs/#/parsing/string-format/
Here is your fiddle fixed:
http://jsfiddle.net/K5ub8/9/
However, I am not 100% sure about the fractional seconds, I believe it is SSS instead of fffffff but I would test this if you need to cater for fractional seconds.
I should mention that if you are converting it back into a JavaScript date object anyway with toDate(), then you don't really need the moment formatting parameter as the date will be formatted in JSON Date format.
I would question why you would want to generate a moment formatted date, and then convert it back to JavaScript, a normal practice might be to receive a date in JavaScript format, then create a moment object which you can use to perform calculations and display in a nice user friendly way.
Simple answer: your format was off a bit.
http://jsfiddle.net/K5ub8/8/
After tweaking the format to be 'YYYY-MM-DDTHH:mm:ss.SSSZZ' rather than 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' it worked just fine. When you're trying to debug issues like this, it's always good to keep the format in a separate variable so you can use the same format that you're trying to parse out to display what you're getting. Had you done that, you would have noticed that 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' was messed up due to it printing out 2014-01-Fr"T"11:32:03.fffffff"-08:00". Which obviously isn't quite right.

Substracting Dates in Javascript return a negative number

I am using the TimeTracker.js from link text to track Page Load times and put them in Google Analytics. Basically what it does is record a start time, and once the page loads it records a end time and then subtracts. These are recored using (new Date()).getTime().
Everything works fine except for instances where the time difference is between 0-100ms. Here I get a massive negative numbers such as -17,183,398,582. Does anyone know what is causing this? Is it got to do with the way Javascript is handling the date substraction or is it something to do with Analytics?
Any help much appreciated. Thanks
Just a guess, but that negative number sounds like it could be linked to Unix epoch time. Example:
var currentTime = new Date().getTime();
currentTime will hold a number such as 1289985468 which represents "GMT: Wed, 17 Nov 2010 09:17:48 GMT".
Perhaps there's a bug in that code you're using.

Categories

Resources