DatePicker value loses a day when string is conver

#1

I’m grabbing the value of the datePicker with this:

var startDate = p.fieldValues["myDate"].toString("yyyy-MM-dd");

I’m then looping 7 days… and in order add the iteration to the date I’m doing this:

duck = new Date(startDate);
tmpDate = duck.addDays(i).toString("MMM dd");

Date.prototype.addDays = function(days) {
		var date = new Date(this.valueOf());
		// console.log("date in function: " + this.valueOf());
		date.setDate(date.getDate() + days);
		return date;
	}

the problem I am running into is that if the date pulled from the picker is “2019-05-13” when I pass that into the new Date() It’s giving me a date 4 hours earlier "Sun May 12 2019 20:00:00 GMT-400 (EDT)

any suggestions?

#2

I was able to get it showing the correct date/time by using this :

duck = new Date(startDate.replace(/-/g, '\/')); 

Apparently the date having “/” in it instead of “-” returns it properly without a timezone offset

¯\(ツ)

#3

Note that Drafts ships with the Date.js library, which makes date operations pretty easy. No need for your addDays function, it’s already available out the box in Drafts - along with a lot of other handy date functions.

I would tend to discourage you from going back and forth between dates and strings unless necessary, because you can get some unpredictable results. p.fieldValues["myDate"] will just return a date from a Date Picker field, no need to make It a string and then a date again.

#4

ah,. gotcha. I was converting it to strings for display purposes, but I can do that later when I put it into the calendar bits… Thanks for the reply.