1

Using Intl.DateTimeFormat I can get the word 'at' between the date and time using dateStyle and timeStyle as shown below. Is it possible to get the exact same output but without the year - "December 20 at 2:23 PM"?

date = new Date(Date.UTC(2020, 11, 20, 3, 23, 16, 738));
new Intl.DateTimeFormat('en-US', { dateStyle: 'long', timeStyle: 'short' }).format(date);
=> "December 20, 2020 at 2:23 PM"

Clarification edit: This needs to work across all locales "at" just being the English version.

This question is not a duplicate of How to format a JavaScript date or any other ones as it is asking how to achieve a specific formatting not discussed there.

Levsero
  • 591
  • 1
  • 4
  • 14
  • The simple answer is “no”. The formats produced by Int.DateTimeFormat are implementation dependent and are not consistent across languages. That’s the whole point of it. – RobG Feb 18 '21 at 06:40
  • I'm not sure what you mean by it is not consistent across languages. Obviously it won't use the exact same word in different locales but the concept does remain consistent. For example using { dateStyle: 'long', timeStyle: 'short' } will add a word between the date and time in any locale. – Levsero Feb 18 '21 at 10:15
  • “Implementation dependent” means “at the discretion of the developers”, including what “long” and “short” resolve to and whether a word is used to join the two. – RobG Feb 18 '21 at 10:50
  • Can you explain how to do that? I can't see any way to control whether a word is used to join the two other than using dateStyle: 'long', but that forces the year to show. – Levsero Feb 18 '21 at 23:12
  • 1
    You can’t. I think the best use of the Intl object in this case is to use the *formatToParts* method as demonstrated in the accepted answer to the duplicate. How you translate “at” in various languages remains an issue. You could just accept whatever your format options produce and delete the year with something like `.replace(/[-\s\/]?\d{4}/,’’)`. But even the OP does not show the time part on iPad for any browser I tested, it just shows a date like 19/02/2021. This is the flaw that makes the default output from the *format* method virtually unusable. – RobG Feb 18 '21 at 23:21

1 Answers1

-2

https://www.npmjs.com/package/moment

import moment from "moment";

let date = moment().format('MMM DD [at] h:mm A')
//  "Jan 30 at 2:30 PM "
console.log(date)

Akhil
  • 411
  • 4
  • 13
  • That does not have the word at, it would be "Feb 18 3:47 PM" rather than "Feb 18 at 3:47 PM". – Levsero Feb 18 '21 at 04:48
  • sorry please check updated answer .... with ''at" – Akhil Feb 18 '21 at 04:52
  • While this code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive-feedback/upvotes from users, when the code is explained. – Amit Verma Feb 18 '21 at 04:53
  • Sorry have updated the question but it needs to work across all locales not just English haven't tested but I'm assuming this would be for English only? Additionally would prefer not to have to import moment. – Levsero Feb 18 '21 at 06:13