How to find time difference between current date and another date [duplicate] - javascript

This question already has answers here:
Get hours difference between two dates in Moment Js
(17 answers)
Closed 3 years ago.
I'm searching a way to find the time difference between 2 dates using moment js in seconds only.
I want to compare current date with dates that I'm reading from the database.
For example:
//current time
var current_time= moment().format('YYYY-MM-DD HH:mm:ss');
//date from sql query
var starting_date=moment(element.start_date).format('YYYY-MM-DD HH:mm:ss');
// print the dates as they are now
console.log(current_time);
console.log(starting_date);
result:
2019-07-02 18:00:11
2019-05-03 15:59:29
I want to find the difference between these 2 dates in seconds only.
I try to make it work using something like this example i found:
var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days')
but it doesn't worked...
var current_time= moment().format('YYYY-MM-DD HH:mm:ss');
var starting_date=moment(element.start_date).format('YYYY-MM-DD HH:mm:ss');
current_time.diff(starting_date, 'seconds');
it crashes
Any idea how to achieve this?

You can use a.unix() - b.unix()
a.diff(b) will return diff in millisecond, you can div for 1000 to get second.
var a = moment('2019-07-02 18:00:11', 'YYYY-MM-DD HH:mm:ss');
var b = moment('2019-05-03 15:59:29', 'YYYY-MM-DD HH:mm:ss');
second = a.unix() - b.unix();
console.log('diff with second:' + second);
secondbymin = a.diff(b);
console.log('diff with milisecond: ' + secondbymin);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>

Related

Why setDate() is giving the final date in milliseconds in nodejs? [duplicate]

This question already has answers here:
setDate() returns number 1603240915215 instead of a date
(2 answers)
Closed 2 years ago.
I a trying to save a date to the nextMonth. For that I am first setting the month to next 30 days. But the final output date it is giving me in milliseconds.
I want the date in GMT format strictly.
What can I do for that?
var snm = new Date();
snm = snm.setDate(snm.getDate() + 30);
console.log("snm = "+ snm);
Try this
var snm = new Date();
snm.setDate(snm.getDate() + 30)
console.log("snm = "+ snm.toString());

Convert string to date and alter that date

Good day, I am generating 3 dates from a string, I hope the output was:
billing date: 2020/01/11
cutoff start: 2019/11/11
cuttof end: 2019/12/10
but I get the following:
billing date: 2020/11/10
cutoff start: 2019/11/10
cuttof end: 2019/12/10
I would like to know how javascript works with variables or what is the problem since everything is altered
var month = "Jan-20"
var coverage_month_obj = moment(month, 'MMM-YY').toDate();
var billing_date = new Date(coverage_month_obj.setDate(coverage_month_obj.getDate() + 10))
var cutoff_end = new Date(billing_date.setMonth(billing_date.getMonth() - 1))
cutoff_end = new Date(billing_date.setDate(billing_date.getDate() - 1))
var cutoff_start = new Date(billing_date.setMonth(billing_date.getMonth() - 1))
I would like to know how javascript works with variables or what is the problem since everything is altered
Put simply, calling setXXX on a javascript date variable updates that variable in place. ie, it is what we would call "mutable". You might have assumed dates were immutable and did not change in place.
To answer on a better way to achieve your goal, I'd suggest using the other functionality of momentjs to calculate your 3 dates from the given input string.
var month = "Jan-20"
var coverage_month = moment(month, 'MMM-YY');
//Jan-20 I need to convert it into date format and that the day is 11 (2020/01/11) cutoff start, are two months less from that date (2020/11/11) and cutoff end is one month less from Jan-20, but ends on day 10 (2020/12/10)
var billing_date = coverage_month.clone().add(10, 'days');
var cutoff_start = billing_date.clone().subtract(2, 'months');
var cutoff_end = billing_date.clone().subtract(1,'months').subtract(1,'day')
console.log("billing_date",billing_date);
console.log('cutoff_start',cutoff_start);
console.log('cutoff_end',cutoff_end);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

How to convert date to milliseconds by javascript? [duplicate]

This question already has answers here:
How do I get a timestamp in JavaScript?
(43 answers)
Closed 4 years ago.
I have multiple date's for example(25-12-2017) i need them to be converted to milliseconds by javascript
One way is to use year, month and day as parameters on new Date
new Date(year, month [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
You can prepare your date string by using a function.
Note: Month is 0-11, that is why m-1
Here is a snippet:
function prepareDate(d) {
[d, m, y] = d.split("-"); //Split the string
return [y, m - 1, d]; //Return as an array with y,m,d sequence
}
let str = "25-12-2017";
let d = new Date(...prepareDate(str));
console.log(d.getTime());
Doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
var dateTokens = "2018-03-13".split("-");
//creating date object from specified year, month, and day
var date1 = new Date(dateTokens[0], dateTokens[1] - 1, dateTokens[2]);
//creating date object from specified date string
var date2 = new Date("2018-03-13");
console.log("Date1 in milliseconds: ", date1.getTime());
console.log("Date2 in milliseconds: ", date1.getTime());
console.log("Date1: ", date1.toString());
console.log("Date2: ", date2.toString());
In addition to using vanilla javascript, you can also use many libraries to get more functions.
like date-fns, moment.js etc
For example, use moment.js you can convert date to milliseconds by moment('25-12-2017', 'DD-MM-YYYY').valueOf(), more elegant and powerful than vanilla javascript.

Add 30 days to a Current date - JS [duplicate]

This question already has answers here:
How to add days to Date?
(56 answers)
Closed 5 years ago.
I want to add 30 days to current date and get in a format.
Im confused to add a 30 to a date and get new.
Its pure JS Solution needed.
Format : June 10, 2017
var date = new Date(); // Now
date.setDate(date.getDate() + 30); // Set now + 30 days as the new date
console.log(date);
Im assuming you are using datejs?
var newDate = Date.today().add(30).days();
try following:
var yourDate = new Date();
var numberOfDaysToAdd = 30;
console.log(yourDate.setDate(yourDate.getDate() + numberOfDaysToAdd));

Adding time to a date in javascript [duplicate]

This question already has answers here:
Incrementing a date in JavaScript
(19 answers)
Closed 6 years ago.
I have this script;
showDiff();
function showDiff() {
var date1 = new Date("2016/03/14 00:00:00");
var date2 = new Date();
var diff = (date2 - date1);
var diff = Math.abs(diff);
var result;
if (diff > 432000000) {
result = 100 + "%";
} else {
result = (diff/4320000) + "%";
}
document.getElementById("showp").innerHTML = result;
document.getElementById("pb").style.width = result;
setTimeout(showDiff,1000);
}
Now I want to get exactly one week added to date1 when atleast one week has passed since that time. That date has to be saved so that one week later, another week can be added to date1. So basically every monday there has to be one week added to date1. How do I this?
The Date object has both a getDate() and setDate() function (date referring to the day of the month, no the full calendar date), so it's really as simple as getting a Date object and setting its date to +7 days from itself.
Example:
var weekFromNow = new Date();
weekFromNow = weekFromNow.setDate(weekFromNow.getDate()+7);
Just to clarify, the Date object contains a full calendar date and time, with its date property referring just to the day in the month (also different from its day property, which is the day of the week).

Categories

Resources