Formatting DatePicker Date

Hi everyone,

Is there a way to properly display Date Picker dates, which will usually run like this Sun Jun 03 2018 00:00:00 GMT-0500 (CDT) and change them to 2018-05-03 (YYYY-MM-DD)?

I appreciate any help I can get,

Thank You,

Sincerely

Hey Nicolas,

Funny how that works - this is the second date formatting question I answered this morning. So all I have to do is to copy from the other post…

Here are a couple of references where you can learn how to format the date and time just about any way you want:

Yisrael

1 Like

Hi Yisrael,

Thanks for the info,

I converted the date from Sun Jun 03 2018 00:00:00 GMT-0500 (CDT) to 6-4-2018 (M-D-YYYY),
I dug around the references you provided but didn’t find a way to transfrom it into YYYY-MM-DD.

I know this is a small detail, but I need it in this specific format in order to make an API call.

Do you have any suggestions in regards to transforming it into this format?

I share with you my code:


var myDate = new Date($w("#datePicker5").value);
var myEndDate = new Date($w("#datePicker6").value);
var updated_Until = ((myDate.getMonth() + 1) + "-" + myDate.getDate() + "-" + myDate.getFullYear());
var updated_Since = ((myEndDate.getMonth() + 1) + "-" + myEndDate.getDate() + "-" + myEndDate.getFullYear());

Thanks!

I really appreciate your help!

But wait, there´s more: toLocaleDateString(); And if you call now, we will give you a free: https://support.wix.com/en/article/how-to-display-todays-date-on-your-site

2 Likes

And as a bonus… Using Wix Code to Format Dates

1 Like

I got a strange issue with Wix DatePicker control. Please follow the code below;

const date = $w("#datePicker2").value;

const options = {
 	day: "numeric",
    	month: "short",
    	year: "numeric"
};

var birthDate = new Date(date.toLocaleDateString("en-US", options));

console.log(birthDate.getDate());
console.log(1+birthDate.getMonth());
console.log(birthDate.getFullYear());

I am forced to use 1+ for a month for a correct value and strangely the getDate() returns a value for the date starting from 02-Jan-1982 on wards. Any date chosen before 02-Jan-1982 is one date less from the chosen date.

For e.g.
when i select 01-Jan-1982 then console log is;
31
12
1981

when i select 02-Jan-1982 then console log is;
02
01
1982

The DatePicker control requires a 360 testing from 1900 or the control is unreliable at this time. I can be wrong, please check advise.

2 Likes

Hello Everyone!

I apologize for the delay in my response,

I found the solution using a combination of what you guys posted here and what I found at Stack Overflow and other web resources.

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}

var updated_until = formatDate($w("#datePicker6").value);
var updated_since = formatDate($w("#datePicker5").value);

Thanks for your help!

1 Like

@nicolasgpo that was helpful. Thank you!