Excel formula to javascript - javascript

What will be the equivalent of this excel formula in javascript?
=MAX(MIN(EndDate1,EndDate2)-MAX(StartDate1,StartDate2)+1,0)
is this even close?
Math.Max(Math.Min(EndDate2,EndDate1)-Math.Max(StartDate2,StartDate1)+1,0)
What I have tried..
var StartDate1 = new Date(2016,12,30);
var EndDate1 = new Date(2017,10,30);
var QueryStartDate = new Date (2017, 01,30)
var QueryEndDate = new Date (2017, 03,30)
Math.Max(Math.Min(ProjectEndDate,QueryEndDate)-Math.Max(ProjectStartDate,QueryStartDate)+1,0)

var x = Math.max(Math.min(EndDate2,EndDate1) - Math.max(StartDate2,StartDate1)+1, 0)
This is assuming EndDate* and StartDateN are Date objects.

Related

Calculate age in JS given the birth date in "dd/mm/yyyy" format

I am trying to write a function in JavaScript to calculate age.
I wrote a function but it does not give accurate age.
function calculateAge(date) {
var formattedDate = date.split("/")
var birthdateTimeStamp = new Date(formattedDate[2], [1], formattedDate[0])
var currentDate = new Date().getTime();
var difference = currentDate - birthdateTimeStamp;
var currentAge = Math.floor(difference / 31557600000)
// dividing by 1000*60*60*24*365.25
return currentAge
}
I try calculateAge("25/11/1993") and I don't get accurate age. Kindly guide me.
You missed the formattedDate for [1] in the new Date() function, so formattedDate[1] it should be.
function calculateAge(date) {
var formattedDate = date.split("/")
var birthdateTimeStamp = new Date(formattedDate[2], formattedDate[1], formattedDate[0])
var currentDate = new Date().getTime();
var difference = currentDate - birthdateTimeStamp;
var currentAge = Math.floor(difference / 31557600000)
// dividing by 1000*60*60*24*365.25
return currentAge
}
var age = calculateAge('25/11/1993');
console.log('My age is', age);

How to keep original date format after manipulation?

I have a reference date like this :
2018-05-23T17:02:59+02:00
I need to substract 1 (milli)second from this reference date, then return it on the exact same format, like this :
2018-05-23T17:02:58+02:00
I succeeded going to the previous second of the same date like this :
var refTime = '2018-05-23T17:02:59+02:00';
var d = new Date(refTime);
var nd = d.setMilliseconds(-1);
But I can't find how to output nd on the exact same format as refTime.
How could I do ?
UPDATE: Just substract 1 to the date.
// 1ms difference
d.getMilliseconds() // 999
newdate2.getMilliseconds() // 998
var refTime = '2018-05-23T17:02:59+02:00';
var d = new Date(refTime);
var nd = d.setMilliseconds(-1);
// new date
var newdate = new Date(d - 1);
console.log(newdate);
//Get second and set subtracting a second
var refTime = '2018-05-23T17:02:59+02:00';
var d = new Date(refTime);
var nd = d.setSeconds(d.getSeconds() - 1);
console.log(new Date(nd))
//Using moment js
var refTime = '2018-05-23T17:02:59+02:00';
var d = new moment(refTime);
var nd = d.subtract(1, 'seconds')
console.log(new moment(nd))
<script src="https://momentjs.com/downloads/moment.min.js"></script>

convert date to number in javascript

I'm using google app script for some application and in javascript m getting NaN error
var state = $("#state").text();
var now = new Date();
var startDate = $("#startDate").val();
var endDate = $("#endDate").val();
var sdate=Number(startDate);
var edate=Number(endDate);
var empName = $("#searchEmpName").val();
alert(edate+","+sdate);
if (edate < sdate){
// show error msg
}
You can use var sdate = parseInt(startDate); instead of var sdate = Number(startDate); and same for the end date.
var sdate=new Date(startDate);
var edate=new Date(endDate);
Here no need to convert sdate and edate in Number.
You can compare both the dates now as you have done before.

new Date() function returns wrong value

The new date(code) returns working value,Please Help me
var cdt = new Date();
dob = "15/01/1999";//From date picker
alert(dob);
var bdy = dob.split("/");
var by = bdy[2];
var bm = bdy[0];
var bd = bdy[1];
var dob = new Date(bd, bm, by);
alert(bd+","+bm+","+by);
alert(dob);
Date format changed for new date() function:
Values return by that function:
new Date() method takes three parameters on constructor.
The order of parameters is following: year,month and day.
Something like this: var date=new Date(1999,01,01).
var cdt = new Date();
dob = "15/01/1999";//From date picker
var bdy = dob.split("/");
var by = bdy[2];
var bm = bdy[1];
var bd = bdy[0];
var dob = new Date(by, (bm-1), bd);
console.log(bd+","+bm+","+by);
console.log(dob.toLocaleDateString());
You could use JavaScript ISO Dates format that is the format: yyyy-mm-dd, see following example please:
var dString = "15/01/1999";
console.log("From date picker", dString);
var bdy = dString.split("/").reverse().join("-")
var dob = new Date(bdy);
console.log("Javascript Date" , dob);
I hope it helps you, bye.

javascript calculating difference between two dates gives me NaN

var startDate = new Date($('#startdate').val());
var endDate = new Date($('enddate').val());
var msPerDay = 1000*60*60*24;
var diff = Math.floor(startDate.getTime() - endDate.getTime());
$('#period').text(diff/msPerDay);
This is my code, it's pretty much based on other answers on SO. I can't seem to figure out what I'm doing wrong here ?
You have typo in your code:
Replace $('enddate').val() with $('#enddate').val()
var startDate = new Date($('#startdate').val());
var endDate = new Date($('#enddate').val()); //using id for enddate?
Are you sure you meant
var endDate = new Date($('enddate').val());
and not
var endDate = new Date($('#enddate').val());
?

Categories

Resources