Den här webbplatsen har begränsad funktionalitet medan vi utför underhåll för att förbättra din upplevelse. Om en artikel inte löser ditt problem och du vill ställa en fråga har vi vår gemenskap som väntar på att hjälpa dig på @FirefoxSupport på Twitter, /r/firefox på Reddit.

Sök i support

Akta dig för supportbedrägerier: Vi kommer aldrig att be dig att ringa eller skicka ett sms till ett telefonnummer eller dela personlig information. Rapportera misstänkt aktivitet med alternativet "Rapportera missbruk".

Läs mer

Date.prototype.toJSON()

  • 2 svar
  • 0 har detta problem
  • Senaste svar av A Ka

more options

Unfortunately using a date type toJSON() result is not accurate. It first apply client timezone and then convert it to json string. In my scenario I am simply read from an API which return a none standard datetime string without its timezone (like "2020-11-21 00:00:00"), Then I try to convert it to a date using new Date("2020-11-21 00:00:00"). Then I use it in another web service but it apply my client timezone when I use JSON.stringify() method (or toJSON()). Does this behaviour is intentionally?


I read the other same question but I am not okay with it. https://support.mozilla.org/en-US/questions/1414380

Unfortunately using a date type toJSON() result is not accurate. It first apply client timezone and then convert it to json string. In my scenario I am simply read from an API which return a none standard datetime string without its timezone (like "2020-11-21 00:00:00"), Then I try to convert it to a date using new Date("2020-11-21 00:00:00"). Then I use it in another web service but it apply my client timezone when I use JSON.stringify() method (or toJSON()). Does this behaviour is intentionally? I read the other same question but I am not okay with it. https://support.mozilla.org/en-US/questions/1414380

Alla svar (2)

more options

I started to Use (var myDate=new Date("2020-11-21 00:00:00");myDate.setHours(myDate.getHours()+6)) to compensate that effect!

Hjälpsam?

more options

As I read in the web, Date constructor does not cover timezone but getters of date object consider client time zone. Altough I think to json is not a simple client side getter since it has z at the end. The same think apply to toISOString() method which also consider client time zone! Of course the other alternative for my scenario is myDate.format("yyyy-MM-ddTHH:mm:ssZ")

Hjälpsam?

Ställ en fråga

Du måste logga in på ditt konto för att svara på inlägg. Vänligen starta en ny fråga om du inte har ett konto ännu.