将YYYYDDD转换为DD-MM-YYYY。

huangapple go评论80阅读模式
英文:

How to transform YYYYDDD to DD-MM-YYYY

问题

有没有moment.js或其他库提供的转换器,可以将以YYYYDDD格式给出的日期转换为现代日期,如DD-MM-YYYY或类似的格式?

英文:

I have a date which is given in YYYYDDD format, where YYYY is the year and DDD the days through the year (001-365).

Does the library from moment.js or any other library has a conversor for this kind of given dates to a modern date, like DD-MM-YYYY or something similar?

答案1

得分: 1

请看文档中的部分:https://momentjscom.readthedocs.io/en/latest/

// 解析格式为 YYYYDD 的字符串
let momentdate = moment('2023050', "YYYYDDD");

// 转换为格式为 DD-MM-YYYY
let datestring = momentdate.format("DD-MM-YYYY");

console.log(datestring);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
英文:

See the moment-documentation: https://momentjscom.readthedocs.io/en/latest/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// Parse string in format YYYYDD
let momentdate = moment(&#39;2023050&#39;, &quot;YYYYDDD&quot;);

// Convert to format DD-MM-YYYY
let datestring = momentdate.format(&quot;DD-MM-YYYY&quot;);

console.log(datestring);

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月9日 23:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定