My use case is very simple, How do i deduct the current time in 24 hours. Technically it is like snapchat, 24 hours from now it will dissapear, hence why I need to deduct the time.
11:50 PM - 10 Jan 2017
I want to deduct the current time to the next 24 hours time only
(10:50 PM - 11 Jan 2017) - (11:50 PM 10 Jan 2017) = 1 hour left
How would I do such thing in Moment.js ?
My use case is very simple, How do i deduct the current time in 24 hours. Technically it is like snapchat, 24 hours from now it will dissapear, hence why I need to deduct the time.
11:50 PM - 10 Jan 2017
I want to deduct the current time to the next 24 hours time only
(10:50 PM - 11 Jan 2017) - (11:50 PM 10 Jan 2017) = 1 hour left
How would I do such thing in Moment.js ?
Share edited Jan 8, 2017 at 14:37 sinusGob asked Jan 8, 2017 at 14:06 sinusGobsinusGob 4,31314 gold badges50 silver badges86 bronze badges 2- Your question is somewhat vague. Could you add some more examples and include the code you tried? – pzp Commented Jan 8, 2017 at 14:25
- @pzp I added more explanation – sinusGob Commented Jan 8, 2017 at 14:37
4 Answers
Reset to default 3You can add -24
hours. See moment.add
moment(date).add(-24, 'hours');
And to display relative time you can use moment.fromNow
I think this would help you :
//valid formats to subtract
moment().subtract(String, Number);
moment().subtract(Number, String); // 2.0.0
moment().subtract(String, String); // 2.7.0
moment().subtract(Duration); // 1.6.0
moment().subtract(Object);
//easy examples
var myString = "03:15:00",
myStringParts = myString.split(':'),
hourDelta: +myStringParts[0],
minuteDelta: +myStringParts[1];
date.subtract({ hours: hourDelta, minutes: minuteDelta});
date.toString()
Read http://momentjs./docs/#/parsing/ for documentation on moment js parse.
This just might work. It did work in JSFiddle http://jsfiddle/Bjolja/p2bcm2oa/
var dt = moment("12:15 AM", ["h:mm A"]).format("HH:mm");
Yes you should use moment.js take a look here
Moment from method
In fact you can deduct time passed between to dates like this
var start = moment("10:50 PM - 11 Jan 2017");
var end = moment("11:50 PM 10 Jan 2017");
start.from(end); // "1 hour"
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742400367a4436792.html
评论列表(0条)