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

Support formatting milliseconds with Intl.DateTimeFormat API #300

Closed
HolgerJeromin opened this issue Nov 27, 2018 · 5 comments
Closed

Support formatting milliseconds with Intl.DateTimeFormat API #300

HolgerJeromin opened this issue Nov 27, 2018 · 5 comments

Comments

@HolgerJeromin
Copy link

@HolgerJeromin HolgerJeromin commented Nov 27, 2018

I need to format a Timestamp in a specific locale (not utc, not browser locale). But I must have the millisecond part of the date, too. My first attempt was second:'numeric' with the DateTimeFormat API:

new Intl.DateTimeFormat(
    'de-DE', // german as an example, user selectable
    { 
        year: 'numeric', month: 'numeric',  day: 'numeric', 
        hour: 'numeric', minute: 'numeric', 
        second: 'numeric',
        hour12: false
    }
)
.format(new Date()); // Date as an example

But the result is something like 26.11.2018, 09:31:04 and not 26.11.2018, 09:31:04,243.

Asked first at stackoverflow.

@ray007
Copy link

@ray007 ray007 commented Nov 27, 2018

Or maybe something more generic for the sub-second part, like the "S" format in ICU.
See http://userguide.icu-project.org/formatparse/datetime and https://www.unicode.org/reports/tr35/tr35-dates.html#dfst-second

@chriscalo
Copy link

@chriscalo chriscalo commented May 17, 2020

Is this getting resolved via #347? That seems to match the API in MDN's docs of Intl.DateTimeFormat: (paraphrased)

// sometimes you want to be very precise
var options = {
  hour: 'numeric', minute: 'numeric', second: 'numeric', 
  timeZone: 'Australia/Sydney',
  timeZoneName: 'short',
  fractionalSecondDigits: 3,
};
console.log(new Intl.DateTimeFormat('en-AU', options).format(date));
// → "2:00:00.200 pm AEDT"

An alternative that more closely matches the other option values ('short', 'long', etc) and also matches the output for fractionalSecond ('.0', '.00', '.000') in MDN's Intl.DateTimeFormat.prototype.formatToParts() docs.

// sometimes you want to be very precise
var options = {
  hour: 'numeric', minute: 'numeric', second: 'numeric', 
  timeZone: 'Australia/Sydney',
  timeZoneName: 'short',
  
  // 'narrow' => '.0'
  // 'short' => '.00'
  // 'long' => '.000'
  fractionalSecond: 'long',
};
console.log(new Intl.DateTimeFormat('en-AU', options).format(date));
// → "2:00:00.200 pm AEDT"
@ray007
Copy link

@ray007 ray007 commented May 18, 2020

Seems that way.
Did someone start a competition for longest possible option name for new things?

@sffc
Copy link
Collaborator

@sffc sffc commented May 19, 2020

Yes, this is #347. Sorry for the long name; we arrived at fractionalSecondDigits because we want it to eventually scale up to 9 digits for nanoseconds when the Temporal proposal comes along

@HolgerJeromin
Copy link
Author

@HolgerJeromin HolgerJeromin commented Jan 6, 2021

Thank you for all your work. Especially @FrankYFTang !

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

Successfully merging a pull request may close this issue.

None yet
5 participants