Momentjs将总周数转换为月缩写(javascript)

我得到了大量的序列号,其中包括制造日期作为星期。

示例序列号:

ABC D 08 05 1234

  • 其中 08 是年份(2008)
  • 05 是一周(01-1st,02-第二,03-第三,04-第四,05-第五周等。)

我想将其转换为 2008年2月格式。有什么简单的方法吗?请帮帮我。

谢谢。

swallowliu1231 回答:Momentjs将总周数转换为月缩写(javascript)

使用format()

[]-以格式字符串转义字符

YY-年

ww-一周

console.log(moment('ABC D 08 05 1234','[ABC D] YY ww [1234]').format('YYYY MMM'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

,

最简单的解决方案是使用输入字符串和parsing tokens中的适当值:

let s = 'ABC D 08 05 1234';

console.log(
  moment(s.substr(6,5),'YY ww').format('YYYY MMM')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

但是,假定默认情况下,周数是根据moment.js使用的方案编号的。

  • 'w'使用“语言环境”周,无论可能是什么
  • “ W”使用ISO 8601星期,该星期指定为从一周的星期一开始,并在一年中的第一个星期四开始。
,

您可以尝试以下

var date=moment('ABC D 08 05 1234','[] YY ww []').format('MMM YYYY');  

document.write(date);
本文链接:https://www.f2er.com/3161736.html

大家都在问