Sqrrl / wc-datepicker

A small, accessible and customizable datepicker written in TypeScript.
https://sqrrl.github.io/wc-datepicker
MIT License
54 stars 6 forks source link

Set default value from html #17

Closed MauricioOnetto closed 10 months ago

MauricioOnetto commented 1 year ago

Hey Great work, it looks awesome Thanks for your work

I'm trying to use and it works pretty sharm, but I can't set a default value. "start-date" shows the correct month/year, but doesn't show any day selected maybe I'm using wrong "value"

This is my code <wc-datepicker locale="es" first-day-of-week="1" start-date="1971-02-27" value={"1971-02-27"} id="Fecha1"></wc-datepicker>

Everything else works perfect Thanks again

MauricioOnetto commented 1 year ago

Using javascript (I would like to doit using only html) I can set a value, but doesn't show the right day

document.getElementById('Fecha1').value = new Date('1971-02-27');

But shows selected: 26

Sqrrl commented 1 year ago

Hey.

The start-date is used to define the initial year and month displayed by the picker. It doesn't set a value. Setting the value property is the correct way. Right now, the value property only accepts Date objects, so setting it via the value attribute on the HTML element doesn't work. Maybe we should add support for string values to be more aligned with native HTML input elements.

document.getElementById('Fecha1').value = new Date('1971-02-27');

This approach should work. It displaying an incorrect date could be a bug. Could you tell me your time zone (Intl.DateTimeFormat().resolvedOptions().timeZone) and offset (new Date().getTimezoneOffset())?

rmathr commented 1 year ago

Using javascript (I would like to doit using only html) I can set a value, but doesn't show the right day

document.getElementById('Fecha1').value = new Date('1971-02-27');

But shows selected: 26

Hey, I was having the same issue of showing the day offset by one. What solved for me was including a replace statement to change '"-" by "/" as follows:

datePicker.value = new Date(${currentDate.replaceAll('-', '/')}). Where 'datePicker' is my wc-datepicker element and 'currentDate' is the date string I want to be set as default.

MauricioOnetto commented 1 year ago

Hey.

The start-date is used to define the initial year and month displayed by the picker. It doesn't set a value. Setting the value property is the correct way. Right now, the value property only accepts Date objects, so setting it via the value attribute on the HTML element doesn't work. Maybe we should add support for string values to be more aligned with native HTML input elements.

document.getElementById('Fecha1').value = new Date('1971-02-27');

This approach should work. It displaying an incorrect date could be a bug. Could you tell me your time zone (Intl.DateTimeFormat().resolvedOptions().timeZone) and offset (new Date().getTimezoneOffset())?

Hey Thanks for your help

(Intl.DateTimeFormat().resolvedOptions().timeZone) = America/Buenos_Aires

(new Date().getTimezoneOffset()) = 180

MauricioOnetto commented 1 year ago

Using javascript (I would like to doit using only html) I can set a value, but doesn't show the right day document.getElementById('Fecha1').value = new Date('1971-02-27'); But shows selected: 26

Hey, I was having the same issue of showing the day offset by one. What solved for me was including a replace statement to change '"-" by "/" as follows:

datePicker.value = new Date(${currentDate.replaceAll('-', '/')}). Where 'datePicker' is my wc-datepicker element and 'currentDate' is the date string I want to be set as default.

Hey I'm doing tests and apparently this change works Thank you