MomentJs subtract time to actual - javascript

I need to subtract actual time from a time variable (formatted in hh:mm:ss) and obtain the result in seconds, for example:
var time1 = "13:40:00";
var time1 = moment(time1, "HH:mm:ss");
var timeNow = ?
var time2 = time1 - timeNow // expressed in s
How can I achieve this?

I am not sure if I understood your needs fully but give this a try
var time1 = "13:40:00";
var time1 = moment(time1, "hh:mm:ss");
var time2 = time1.diff(moment(), 'seconds')

You can use moment diff to get the difference between two moment objects. In your case:
var time1 = '13:40:00';
var diff = moment().diff(moment(time1, 'HH:mm:ss'), 'seconds');
console.log(diff);
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>

Try this:
var time1 = moment.duration("24:40:00", "HH:mm:ss").asSeconds();
var timeNow =moment.duration(moment().format('HH:mm:ss'),"HH:mm:ss").asSeconds();
var log = time1-timeNow;

Related

How to find time difference in seconds using moment.js

How can I find time difference in seconds between two different timezones.
I want to use moment for this -
My start time is something like- 2022-09-04T07:29:39Z[UTC] and end time will be the current time in my frontend.
My code =
var now = new Date().getTime();
var then = "2022-03-04T07:29:39Z[UTC]";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss")); //NAN
var d = moment.duration(ms);
var s = d.format("hh:mm:ss");
console.log("Time Difference =",s);
I need help in this, currently I am getting ms as NAN how can I correct it!
This type of calculation can easily be done with Vanilla JavaScript:
const now = new Date().getTime(),
then = new Date("2022-03-04T07:29:39Z"),
tsec= Math.round((now-then)/1000),
sec=tsec%60, tmin=(tsec-sec)/60,
min=tmin%60, th=(tmin-min)/60,
h=th%24, d=(th-h)/24;
console.log(`Time Difference = ${d}d ${h}h ${min}m ${sec}s`);
d.format is not a function exposed by momentjs instead of that you should write like below
var now = new Date().getTime();
var then = "2022-03-04T07:29:39Z[UTC]";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = moment(d).format('hh:mm:ss')
console.log("Time Difference =",s);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

get the time difference between two times in format hh:mm:ss

I have two times in HH:mm:ss format. I am trying to calculate the time difference between two times.
like
var timeStart = '01:00:24';
var timeEnd = '01:00:34'
var timeDiff = timeEnd - timeStart;
How can i perform this with a javascript
You can split them with : and take difference. And again join them with :
var start = '01:00:24';
var end = '01:00:34';
var diff = start.split(':').map((item,index) => end.split(':')[index] - item).join(':')
console.log(diff)
you can prepone to both a fixed date and do the subtraction:
var start = '01:00:24';
var end = '01:00:34';
start = '2017-11-8' + start;
end = '2017-11-8' + end;
var diff = +end - +start; // outputs 10000 (in ms)

Node / Javascript Set Date Time

I have parameters startTime and endTime which are in the format of:
var time1 = "08:00";
var time2 = "23:00";
I need to compare whether the current date/time is in between these two periods:
var now = new Date();
if (now > time1 && now < time2)
{
console.log("We are in the time period");
}
It is impossible to change the input of time1 or time2.
I was thinking abotu converting time1 and time2 to a new Date format, however I am unsure how to go about this.
How would you go about it?
Thanks
You should do something like this:
var time1 = parseInt("08:00".substring(0,2));
var time2 = parseInt("23:00".substring(0,2));
var now = new Date();
if (now > new Date().setHours(time1) && now < new Date().setHours(time2))
{
console.log("We are in the time period");
}

calculate difference between two timestamp and get difference in unix timestamp

I want to calculate the difference between two dateTime, one date is submitted by user and other is current time:
user submitted time - now = difference in unix
user submitted time format is:
2014-03-26 10:52:00
Thanks for your help.
You can simply do this with getTime() which returns the number of milliseconds.
var ds = "2014-03-26 10:52:00";
var newDate = new Date(ds).getTime(); //convert string date to Date object
var currentDate = new Date().getTime();
var diff = currentDate-newDate;
console.log(diff);
Sometimes there are chance for cross browser compatibility in parsing the date string so it is better to parse it like
var ds = "2014-03-26 10:52:00";
var dateArray = ds.split(" "); // split the date and time
var ds1 = dateArray[0].split("-"); // split each parts in date
var ds2 = dateArray[1].split(":"); // split each parts in time
var newDate = new Date(ds1[0], (+ds1[1] - 1), ds1[2], ds2[0], ds2[1], ds2[2]).getTime(); //parse it
var currentDate = new Date().getTime();
var diff = currentDate - newDate;
console.log(diff); //timestamp difference
You can use MomentJS library
var user_submited_time = moment('2014-03-26 10:52:00');
var now = moment();
var value = user_submited_time - now;

Calculate difference between two calendar dates in javascript

I need to calculate difference between two calendar dates. I have gone through various posts but the value returned is not correct.
Heres' my code:-
function getTotalDays()
{
var date11 = document.getElementById("departure_date").value;
var date22 = document.getElementById("arrival_date").value;
var one_day=1000*60*60*24;
var date1 = new Date(date11);
var date2 = new Date(date22);
// Convert both dates to milliseconds
var date1_ms = date1.getTime();
var date2_ms = date2.getTime();
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
var diffDays = Math.round(difference_ms/one_day);
alert(diffDays);
}
suppse the difference is 2 days its showing as 59.
What's wrong..??
The values you are passing to the date object are likely wrong. Its probably easier for you to do something like this:
var date1 = getDate(date11);
var date2 = getDate(date22);
with getDate being:
function getDate(date) {
//date format dd/mm/yyyy
var dateArr = date.split('/');
var date = new Date(dateArr[2], dateArr[1], dateArr[0]);
return date;
}

Categories