moment().format("LTS") returns a string value in hh:mm:ss AM/PM format.
When you create a moment object using a string that is not in standard format, you should pass the input format as second parameter to moment constructor.
For eg: Jan 1, 2017 in string 01012017 is not a standard representation. But if you need a moment object out of it, using moment("01012017") will give "Invalid Date" response when formatting. Instead, use moment("01012017","DDMMYYYY")
var d = moment("01012017")
d.toISOString() => "Invalid date"
var d = moment("01012017", "DDMMYYYY")
d.toISOString() => "2016-12-31T18:30:00.000Z"
In your code, when creating 'date' variable pass "hh:mm:ss A" as second parameter in the moment constructor as mentioned below .
var date = moment(startdate, "hh:mm:ss A")
.add(seconds, 'seconds')
.add(minutes, 'minutes')
.format('LTS');
Answer from Muralidharan on Stack Overflowmoment().format("LTS") returns a string value in hh:mm:ss AM/PM format.
When you create a moment object using a string that is not in standard format, you should pass the input format as second parameter to moment constructor.
For eg: Jan 1, 2017 in string 01012017 is not a standard representation. But if you need a moment object out of it, using moment("01012017") will give "Invalid Date" response when formatting. Instead, use moment("01012017","DDMMYYYY")
var d = moment("01012017")
d.toISOString() => "Invalid date"
var d = moment("01012017", "DDMMYYYY")
d.toISOString() => "2016-12-31T18:30:00.000Z"
In your code, when creating 'date' variable pass "hh:mm:ss A" as second parameter in the moment constructor as mentioned below .
var date = moment(startdate, "hh:mm:ss A")
.add(seconds, 'seconds')
.add(minutes, 'minutes')
.format('LTS');
Moment has really good documentation. I would check it out: http://momentjs.com/docs/
But to address your question more directly, you could do something like:
var secondsToMinutes = '3:20';
var seconds = secondsToMinutes.split(':')[1];
var minutes = secondsToMinutes.split(':')[0];
var momentInTime = moment(...)
.add(seconds,'seconds')
.add(minutes,'minutes')
.format('LT');
You should use the actual handlers to the best of your ability. There are some cool things you can do with durations now, but this is more succinct.
Edit:
As mentioned in a different answer:
moment('2:00:00 PM', 'h:mm:ss A')
Is necessary if you're handling that format. Regardless - adding/subtracting minutes/hours to a moment object is trivial. Passing invalid strings to a moment object is a different issue in-and-of itself. ;)
I think you missed a key point in the documentation for .add()
Mutates the original moment by adding time.
You appear to be treating it as a function that returns the immutable result. Easy mistake to make. :)
If you use the return value, it is the same actual object as the one you started with. It's just returned as a convenience for method chaining.
You can work around this behavior by cloning the moment, as described here.
Also, you cannot just use == to test. You could format each moment to the same output and compare those, or you could just use the .isSame() method.
Your code is now:
var timestring1 = "2013-05-09T00:00:00Z";
var timestring2 = "2013-05-09T02:00:00Z";
var startdate = moment(timestring1);
var expected_enddate = moment(timestring2);
var returned_endate = moment(startdate).add(2, 'hours'); // see the cloning?
returned_endate.isSame(expected_enddate) // true
I am working on an application in which we track live route. Passenger wants to show current position of driver and the expected arrival time to reach at his/her location. So I need to add some duration into current time.
So I found the below mentioned way to do the same. We can add any duration(hour,minutes and seconds) in our current time by moment:
var travelTime = moment().add(642, 'seconds').format('hh:mm A');// it will add 642 seconds in the current time and will give time in 03:35 PM format
var travelTime = moment().add(11, 'minutes').format('hh:mm A');// it will add 11 mins in the current time and will give time in 03:35 PM format; can use m or minutes
var travelTime = moment().add(2, 'hours').format('hh:mm A');// it will add 2 hours in the current time and will give time in 03:35 PM format
It fulfills my requirement. May be it can help you.
format returns a string, you have to use add on moment object.
Your code could be like the following:
var hours = randomIntFromInterval(0,23);
var minutes = randomIntFromInterval(0,59);
var time = moment(hours+':'+minutes,'HH:mm');
time.add(7,'m');
console.log(time.format("HH:mm"));
Note that you can create a moment object using moment(Object) method instead of parsing a string, in your case:
moment({hours: hours, minutes: minutes});
As the docs says:
Omitted units default to 0 or the current date, month, and year
You can use IF condition to see if minutes < 10
to add "0" like 07,08
var d = new Date() var n = d.getMinutes() if(n<10) n="0"+n;
But watch out you will have to slice that zero if you want to increment.
EDIT: I was young when wrote this. A better approach is: moment(YOUR_DATE).format('HH:mm')
First of all you need to parse the string data, we know we're using a valid hour.
const time = '15:30' // Valid date
const time = '252:10' // Invalid date
You can use a second parameter in the moment function, something like this:
moment(time, "HH:mm");
You simply need to know what format to convert your string to, read more about that in this documentation link.
Then, you can occupy the different builders to get the expected result.
moment(time, "HH:mm") // Parse the string
.add(40, 'minutes') // Add 40 minutes
.add(2, 'hours') // Add 2 hours
.format('HH:mm') // Format to specified format
And finally you will get your date time parsed.
Here is a working version of this:
const time = '15:30'
const date = moment(time, "HH:mm").toDate();
const parsedDate = moment(date)
.add(40, 'minutes')
.add(2, 'hours')
.format('HH:mm')
console.log(parsedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
Adding 2 hours and 30 minutes to 15:30h will get you 18:10h - also without using moments.js:
const time=new Date(2022,1,1,15,30); // define initial datetime object
time.setHours(time.getHours()+2);
time.setMinutes(time.getMinutes()+40);
console.log(time.toTimeString().slice(0,5));
Another way of solving the problem, involving a Date.prototype definition could be:
Date.prototype.add=function(n,unit){ this"set"+unit+n); return this; }
console.log(new Date(2022,1,1,15,30)
.add(2,"Hours")
.add(40,"Minutes")
.toTimeString()
.slice(0,5) );
Before the protest comes in from all sides: Yes, I get it: extending the prototype of a built in class is not considered good programming style. The above example should therefore be seen as a demonstration of how the prototype function mechanism works in JavaScript and should probably not be incorporated in real projects.