2

I am getting the value of date time from server in a variable a1:

let a1 = (new Date(estimatedServerTimeMs));

console.log of a1 Sun Apr 05 2020 11:36:56 GMT+0530 (India Standard Time)

I break it to a simple date format like this 5-3-2020 but i want it to be like 05-APR-2020

Below is my code:

 let a1 = (new Date(estimatedServerTimeMs));
      console.log(a1);
      let show_year_month_date =  a1.getDate()+'-'+ a1.getMonth() +'-'+ a1.getFullYear();
      console.log(show_year_month_date);

How can I:

  1. change date to 2 digits
  2. change month from numeric to 3 character value
halfer
  • 18,701
  • 13
  • 79
  • 158
user2828442
  • 1,975
  • 2
  • 40
  • 75
  • Do you just want to display the formatted value in HTML, or in code? – Kurt Hamilton Apr 05 '20 at 06:13
  • i need value in a variable `show_year_month_date` – user2828442 Apr 05 '20 at 06:18
  • What version of Angular are you on? – Kurt Hamilton Apr 05 '20 at 06:18
  • ionic 5, angular cli - 8.3 – user2828442 Apr 05 '20 at 06:21
  • I disagree with this being unilaterally closed by palaѕн for the reason stated. There are better ways to format dates in Angular. – Kurt Hamilton Apr 05 '20 at 06:22
  • https://stackoverflow.com/a/50328749/5367916 – Kurt Hamilton Apr 05 '20 at 06:25
  • can you answer as per my code like how do i change from current value to the one i want, i'll accept the answer – user2828442 Apr 05 '20 at 06:36
  • New answers can't be added while the question is closed. The answer I linked to is the approach I would take. This is my implementation of the format you want: https://stackblitz.com/edit/angular-aq389x – Kurt Hamilton Apr 05 '20 at 06:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210971/discussion-between-user2828442-and-kurt-hamilton). – user2828442 Apr 05 '20 at 06:41
  • @halfer I think it would be good for us all to leave this question closed and consign it to history. Adding any kind of answer here isn't going to significantly contribute anything of worth to the archives. It is such a common question that comes in various flavours. I appreciate you taking the time to at least discuss this, and I was a little hasty in accusing the closer of arrogance. I retract that, and will delete my comment. My frustration is more with gold badgers being able to close a question with a single vote - and without explanation. – Kurt Hamilton Apr 05 '20 at 16:56
  • 1
    @Kurt: no worries, and thanks. I like the gold badge system in general. If you see a closure you believe is wrong you can appeal to the badge holder using their `@` name in the comments (as long as they were the only closer, I believe). If you don't get any joy there (can't notify them or they don't respond) then you can raise a question on [Meta Stack Overflow](https://meta.stackoverflow.com/). As long as you present it neutrally and are open to what the community thinks, such questions are usually received very well. – halfer Apr 05 '20 at 17:12
  • 1
    @halfer Good to know, thanks – Kurt Hamilton Apr 05 '20 at 17:22

1 Answers1

1

Try this way

let a1 = (new Date(estimatedServerTimeMs));

const dtf = new Intl.DateTimeFormat('en', { year: 'numeric', month: 'short', day: '2-digit' });
const [{ value: month },,{ value: day },,{ value: year }] = dtf.formatToParts(a1);

let show_year_month_date = day + '-' + month.toUpperCase() + '-' + year;

console.log(show_year_month_date);