Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parseDate on functional bind (working with dates example) doesn't work / documentation confusing #185

Open
LaughingBubba opened this issue Dec 16, 2024 · 1 comment

Comments

@LaughingBubba
Copy link

I pretty much lifted the code from the working-with-dates#example and discovered the parseDate doesn't work or is deprecated. Then I realised the functional example referenced in the migration guide did it differently and more coherently.

Just calling this out as I wasted a fair bit of time on this.

This doesn't work ====>

<script>
  ...
	
  let date = $state(( () => {
		let initDate = new Date()
		initDate.setHours(0,0,0)
		return initDate
	})() )

</script>

    <SveltyPicker
      inputId = "eventdatetime"
      inputClasses = ""
      mode = "date"
      format = "yyyy/mm/dd hh:ii"
      weekStart = {2}
      bind:value ={ () => {
                      return formatDate(date, 'yyyy/mm/dd', en, 'standard')
                    },
                    (v) => {
                      date = v ? parseDate(v, 'yyyy/mm/dd', en, 'standard') : null
                      console.log('raw "v" value', v)
                      console.log("parsed date", (v ? parseDate(v, 'yyyy/mm/dd', en, 'standard') : null))
                      console.log("state date", date)
                    }
                  }
    />

Produces:
image

But this does ====>

<script>
  ...

  let date = $state(new Date())

</script>

    <SveltyPicker
      inputId = "eventdatetime"
      inputClasses = ""
      mode = "date"
      format = "yyyy/mm/dd hh:ii"
      weekStart = {2}
      bind:value ={ () => {
                      return formatDate(date, 'yyyy/mm/dd', en, 'standard')
                    },
                    (v) => {
                      date = new Date(v)
                    }
                  }
    />
@mskocik
Copy link
Owner

mskocik commented Dec 16, 2024

I pretty much lifted the code from the working-with-dates#example

Is something wrong/not working with example itself? Works for me 🤔

This doesn't work ====>

That's because you are parsing date in date-only format, but you are providing date AND time part.
yyyy/mm/dd is NOT the same as yyyy/mm/dd hh:ii. Also I noticed, you are specifying mode as date, but providing datetime format. It is not compatible, and if date fails to parse/format, it returns "now" date as fallback value. Which is your case.

For you to simplify things, remove mode property. It's resolved automatically based on specified format.


To summarize this, parseDate and formatDate works as intended, they are used in library itself. But you need to stick to exactly the same format value when parsing/formatting date, otherwise can get invalid date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants