Locale specific date without year
Asked Answered
D

3

6

I am using https://github.com/abritinthebay/datejs/ for date formatting due to locale support. However, is it not possible to get a full date time without year?

Example

Input date:2014/09/20 20:00:00

Output date: 09/20 20:00

And it has to respect locale settings!

Dispirit answered 30/9, 2014 at 9:0 Comment(1)
Javascript doesn't provide access to locale settings. The best libraries can do is guess the time zone from the timezone offset around the middle and end of the year. There is a built–in Date.prototype.toLocaleString, but the output is entirely implementation dependent and differs widely from host to host. Probably the best you can do is ask users to identify the format they like and remember the choice for later use.Rondo
R
10

Looks like since ES2015 you can just skip first parameter and set only 'options' parameter, in that way locale will be applied:

new Date().toLocaleString(undefined, {
    month: "short", day: "numeric", 
    hour: "numeric", minute: "numeric", second: "numeric"
}) // "Jul 11, 5:50:09 PM"

I didn't find the way to remove comma between date and time. For that case string formatting can be used:

const dateTime = new Date();
const datePart = dateTime.toLocaleDateString(undefined, {month: "short", day: "numeric"});
const timePart = dateTime.toLocaleTimeString();
const result = `${datePart} ${timePart}`;
// "Jul 11 5:57:10 PM"
Raine answered 11/7, 2019 at 14:59 Comment(1)
Noting that toLocaleString only gives the elements requested in the options; that is not entirely obvious.Sturmabteilung
E
0
console.log(new Date().toLocaleString('en-En',{weekday: "long", month: "long", day: "numeric"}))

You can change this options as you want.

Eloisaeloise answered 14/5, 2017 at 18:7 Comment(0)
R
-2

To format a date as month/day hour:minute with Date.js you'd call toString with the format 'MM/DD HH:mm' to get two digits for all values, e.g.:

console.log(new Date().toString('MM/dd HH:mm'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script>

Attempting to determine the format that the user expects to see is very problematic, whether it's referred to as "culture", "locale" or just "preference". Javascript doesn't have access to system settings and the browser doesn't reveal them. You can try to guess based on the output of Date.prototype.toLocaleString, but that is entirely implementation dependent and doesn't necessarily conform to user preferences.

One common approach is to use an unambiguous format so user preferences don't matter, e.g.

console.log(new Date().toString('dd-MMM HH:mm'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script>

Another approach is to have an unambiguous default format, then allow the user to select from a few supported formats and store the preference.

There is also the built–in Date.prototype.toLocaleString, which is pretty unreliable but some browsers support the optional ECMA-402 Intl formatting options. It's pretty ordinary as a formatter so really can't be recommended when there are libraries that do the job so much better, e.g.

var options = {
  month: 'short',
  day  : '2-digit',
  hour : '2-digit',
  minute:'2-digit'
};

// Browser dependent, something like en-us: Jan 21, 8:39 AM
console.log('en-us: ' + new Date().toLocaleString('en-us',options))

// Browser dependent, something like en-gb: 21 Jan, 08:39
console.log('en-gb: ' + new Date().toLocaleString('en-gb',options))

Yet another approach is to write your own parser and formatter that does just what you need. If you only need to support one or two formats, it's pretty straight forward, e.g.

// input format yyyy/mm/dd hh:mm:ss
function parseDateString(ds) {
  var d = ds.split(/\D+/);
  return new Date(d[0], --d[1], d[2], d[3], d[4], d[5]);
}

// Return date string as mm/dd hh:mm
function formatDate(d) {
  function z(n) {
    return (n < 10 ? '0' : '') + n
  }
  return z(d.getMonth() + 1) + '/' + z(d.getDate()) +
    ' ' + z(d.getHours()) + ':' + z(d.getMinutes());
}

console.log(formatDate(parseDateString('2014/09/20 20:00:00'))); // 09/20 20:00

So you can replace an entire library with less than a dozen lines of code. :-)

Rondo answered 30/9, 2014 at 12:12 Comment(3)
I'm not sure this respects the browsers locale as this answer is hardcoding to US date. the Author wants something that will work in any countries locale, as do I.Diamine
@Simon—you can't detect a user's locale other than through geolocation (which is unreliable) and that doesn't necessarily tell you the format that the user expects for dates. You're correct though, this wasn't a good answer. Hopefully the updates make it a better one. Perhaps I should have just said "Don't attempt to determine the user's location and use that to format the date, use an unambiguous format like '20-Jan-2016' instead".Rondo
Think your last sentence would of been an acceptable answer to me.Diamine

© 2022 - 2024 — McMap. All rights reserved.