new Date() function returns wrong value - javascript

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.

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 Convert string time to date object in angular

how to convert string time to date object
var time_t = "09:56 AM" ;
this.audit_time = new Date(time_t);
//Error Invalid date
how do i correct it.please help me to solve this
You need also date part in your string (this will also work with PM):
var time_t = "09:56 AM" ;
var dt = new Date("1990-01-01 "+time_t);
console.log(dt);
dt = new Date(new Date().toISOString().slice(0,10) + " " + time_t);
console.log(dt);
try this
var time_t = "09:56 AM";
var timeArr = time_t.replace(" AM", "").split(":");
var d = new Date();
d.setMinutes(timeArr[1]);
d.setHours(timeArr[0]);
console.log(d);

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.

Excel formula to 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.

Apply function to object filled with numbers

I have this simple function that adds a certain number of days to a given date and gets the new date:
var adddays = 401;
var theDate = new Date(2014, 01, 01);
var myNewDate = new Date(theDate);
myNewDate.setDate(myNewDate.getDate() + adddays);
console.log(myNewDate);
Rather than doing one date at a time, I'm now getting dates in an object like this: {543,563,601,629,650,672,698,718}
The question is how I can run all these days through this function to get an object with newly formatted dates. These numbers in the object would substitute for adddays. I know I need a for loop, but I'm a little new to JS to figure it out.
var theDate = new Date(2014, 01, 01);
var newDates = ([543,563,601,629,650,672,698,718]).map(function (e) {
var adddays = e;
var myNewDate = new Date(theDate);
myNewDate.setDate(myNewDate.getDate() + adddays);
console.log(myNewDate);
return myNewDate;
})
or
var str = "{543,563,601,629,650,672,698,718}";
var theDate = new Date(2014, 01, 01);
var newDates = (str.substr(1,str.length-1).split(',')).map(function (e) {
var adddays = parseInt(e,10);
var myNewDate = new Date(theDate);
myNewDate.setDate(myNewDate.getDate() + adddays);
console.log(myNewDate);
return myNewDate;
})
The string you are receiving is not a valid javascript Object and looks more like an array. We will first have to transform it from a string to an array.
var daysToAdd = "{543,563,601,629,650,672,698,718}";
//Removing the {} brackets to turn it into a CSV
daysToAdd = daysToAdd.substring(1,daysToAdd.length-1)
//Using split to turn our values into an array
daysToAdd = daysToAdd.split(',');
//daysToAdd now looks like ["543","563","601","629","650","672","698","718"]
We will want to make a function that accepts the base data and returns the adjusted dates for re-usability. In our function we can use Array.map to call a function on each element in our array
function addAllDaysToDate(daysToAdd,referenceDate){
var adjustedDays = daysToAdd.map(function(addDays){
var date = new Date();
//+variable is shorthand for parseInt(variable);
date.setDate(referenceDate.getDate() + (+addDays));
return date;
});
//Our adjustedDays is now an array of Date objects
return adjustedDays;
}
We can then use our function to get the adjusted dates
var theDate = new Date(2014, 01, 01);
var myNewDates = addAllDaysToDate(daysToAdd,theDate)

Categories

Resources