Ray M
Ray M

Reputation: 90

how to send date as timestamp in svelte?

The svelte application is supposed to send these data to create an event to be stored in a database.

let eventName=document.querySelector('#eventName')
    let timeFrom=document.querySelector('#timeFrom')
    let timeTo=document.querySelector('#timeTo')
    let repeatedEvery=document.querySelector('#repeatedEvery')
    let startFrom=document.querySelector('#startFrom')
    let endOn=document.querySelector('#endOn')
    const status="upcoming"
    let isRepeated=true

The dates should be sent as UNIX timestamps, I can do that just fine. The problem is to do that I had to add toString() to the variables which gives me an error of "this element is null", without it gives me 5 overload errors. I tried declaring the variable in a function that would do this process but the variables are not global anymore.

I don't want to fix the current code, I'll write the whole thing from scratch if I have to, my question is how do I actually approach this in the first place? the dates input fields are datetime-local, and needless to say the timestamps should be a number, all the other types of data go through just fine.

Upvotes: 0

Views: 873

Answers (1)

cape_bsas
cape_bsas

Reputation: 698

Date.prototype.valueOf() returns what you need.

const dNow = new Date();
dNow.valueOf(); // 1673445066359

If you are getting null values in variables that are supose to be Dates, you can default them to a value.

const dDateDefaultToNow = theSourceValueThatYouAreUsing || new Date();
const dDateDefaultToZero = theSourceValueThatYouAreUsing || 0;

This way, if theSourceValueThatYouAreUsing is null then it will default to new Date() or 0.

For more help, please share more of your code.

Upvotes: 2

Related Questions