spencermountain / spacetime

A lightweight javascript timezone library
http://spacetime.how/
Other
3.97k stars 184 forks source link

Month number is always -1 #375

Closed vresetnikov closed 1 year ago

vresetnikov commented 1 year ago

Hello,

Considering the following tests:

const spacetime = require("spacetime");

const timestamp1 = spacetime("2022-10-01T00:00:00.000Z", "Europe/Rome");

//returns 2022-09 (September??)
const timestamp1_test1 = timestamp1.format('{year}-{month-pad}');
console.log(timestamp1_test1);

//returns 2022-09 (September??)
const timestamp1_test2 = timestamp1.format('{year}-{month-number}');
console.log(timestamp1_test2);

//returns 2022-10-01, desired outcome BUT illegal (format() method requires at least 1 argument)
const timestamp1_test3 = timestamp1.format();
console.log(timestamp1_test3);

Why is it displaying the month number 9 (September) instead of 10 (October)? What is the correct way to achieve the 2022-10-01 format?

Thank you

vresetnikov commented 1 year ago

I have just spotted the iso-month setting, is this the answer? Why this separation? When are month-pad and month-number useful?

iso-month is indicated in the documentation, however I don't think it is not listed in the Format type:

export type Format =
  | 'day'
  | 'day-short'
  | 'day-number'
  | 'day-ordinal'
  | 'day-pad'
  | 'date'
  | 'date-ordinal'
  | 'date-pad'
  | 'month'
  | 'month-short'
  | 'month-number'
  | 'month-ordinal'
  | 'month-pad'
  | 'year'
  | 'year-short'
  | 'time'
  | 'time-24'
  | 'hour'
  | 'hour-pad'
  | 'hour-24'
  | 'hour-24-pad'
  | 'minute'
  | 'minute-pad'
  | 'second'
  | 'second-pad'
  | 'millisecond'
  | 'ampm'
  | 'quarter'
  | 'season'
  | 'era'
  | 'timezone'
  | 'offset'
  | 'numeric'
  | 'numeric-us'
  | 'numeric-uk'
  | 'mm/dd'
  | 'iso'
  | 'json'
  | 'iso-short'
  | 'iso-utc'
  | 'nice'
  | 'nice-year'
  | 'nice-day'
  | 'nice-full'
  | string
spencermountain commented 1 year ago

hey Vladimir - yep, this sucks. Javascript uses 0-based months, and 1-based days. it was a design decision we went with early on. Your timing is good. I'm working on a breaking change, and may try to improve this but yes, for now, 'iso-month' or a rogue +1 should do the trick cheers

vresetnikov commented 1 year ago

hey Vladimir - yep, this sucks. Javascript uses 0-based months, and 1-based days. it was a design decision we went with early on. Your timing is good. I'm working on a breaking change, and may try to improve this but yes, for now, 'iso-month' or a rogue +1 should do the trick cheers

Thank you very much for your continuous effort maintaining this awesome library