【问题标题】:Change month from numeric value to 3 alphabet [duplicate]将月份从数值更改为 3 个字母 [重复]
【发布时间】:2020-07-17 04:57:24
【问题描述】:

我在变量a1 中从服务器获取date time 的值:

let a1 = (new Date(estimatedServerTimeMs));

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

我将其分解为像 5-3-2020 这样的简单日期格式,但我希望它像 05-APR-2020

下面是我的代码:

 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);

我该怎么做:

  1. 将日期更改为 2 位数字
  2. 将月份从数字更改为 3 个字符的值

【问题讨论】:

  • 您只想在 HTML 或代码中显示格式化的值吗?
  • 我需要变量show_year_month_date中的值
  • 你使用的是哪个版本的 Angular?
  • @Kurt:不用担心,谢谢。总的来说,我喜欢金徽章系统。如果您看到您认为错误的关闭,您可以在 cmets 中使用他们的@ 名称向徽章持有者提出上诉(我相信只要他们是唯一的关闭者)。如果您在那里没有得到任何快乐(无法通知他们或他们没有回应),那么您可以在Meta Stack Overflow 上提出问题。只要您中立地提出问题并对社区的想法持开放态度,此类问题通常会很受欢迎。
  • @halfer 很高兴知道,谢谢

标签: javascript angular typescript


【解决方案1】:

试试这个方法

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);

【讨论】:

  • 来了05-Apr-2020Apr怎么会是大写?
  • @user2828442 让 show_year_month_date = day + '-' + month.toUpperCase() + '-' + year;我已经编辑了答案。请检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多