convert date to number in javascript - 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.

Related

Moment.js while loop in javascript, node.js

The code supposes to fill the array with dates between given start and end dates. I am using Node.js.
var startDate = moment(startDay);
var endDate = moment(endDay);
var datesBetween = [];
var startingMoment = startDate;
while(startingMoment <= endDate) {
datesBetween.push(startingMoment);
startingMoment.add(1, 'days');
}
console.log(datesBetween);
You need to create a new object each iteration otherwise you are pushing the same object reference into array each time. They will all end up with last date since it is all the same object
You can use moment.clone() to clone the object each time
var startDay = new Date(2018,1,1);
var endDay = new Date(2018,1,4);
var startDate = moment(startDay);
var endDate = moment(endDay);
var datesBetween = [];
var startingMoment = startDate;
while(startingMoment <= endDate) {
datesBetween.push(startingMoment.clone());// clone to add new object
startingMoment.add(1, 'days');
}
console.log(datesBetween);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.0/moment.min.js"></script>

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.

Get Diffrence between two date string in momentjs?

I am using momentjs for date and I have one date string, ie,"2015-05-10",I want to get date difference from today
var today= moment().format('YYYY-MM-DD');
How it is possible here?
here is a example,
var now = moment(); // moment object of now
var day = moment("2015-05-13"); // moment object of other date
$scope.difference = now.diff(day, 'days'); // calculate the difference in days
$scope.difference = now.diff(day, 'hours'); // calculate the difference in hours
check more options here
here is a example
You can use diff
//Convert to date
var today = moment();
var date = moment("2015-05-13", "YYYY-MM-DD");
//Use diff
var duration = today.diff(date);
var hours = duration.asHours();
if you are talking about time difference in hours
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
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 = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

Adjust file created date value for date between evaluation using Google Apps Script

I want to look at the file created date instead of the current date and add 6 days so my between code will work properly.
var dateFrom = file.getDateCreated();
var dateTo = new Date();
dateTo.setDate(dateTo.getDate() + 6).toString();
var dateCheck = new Date();
dateCheck.setDate(dateCheck.getDate()).toString();
var from = Date.parse(dateFrom);
var to = Date.parse(dateTo);
var check = Date.parse(dateCheck );
if((check <= to && check >= from))
// alert("do something");
I have made a few attempts
var dateTo = (file.getDateCreated() + 7).toString();//failed attempt 1
var dateTo = new Date();
dateTo.setDate(dateTo.file.getDateCreated() + 6).toString();//failed attempt 2
var dateTo = new Date();
dateTo.setDate(dateTo.file.getDateCreated() + 6);//failed attempt 3
I am hoping someone can help me learn how to find success.
Regards,
Chris
This code determines whether today's date is at or past a target date. The target date is 6 days past the file creation date:
function olderThan() {
var file = DriveApp.getFileById('Your file ID');
var myFileDate = file.getDateCreated();
var creationDate = new Date(myFileDate);
var now = new Date();
var dateTo = new Date();
dateTo.setDate(creationDate.getDate() + 6);
if (now >= dateTo) {
Logger.log('its older than 6 days old');
};
};

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