如何格式化BC日期(例如"

编程入门 行业动态 更新时间:2024-10-27 14:26:06
本文介绍了如何格式化BC日期(例如"-700-01-01")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何使用Moment.js格式化ISO日期BC?

How to format ISO dates BC with Moment.js?

moment("-700-01-01").year(); // 700 (WRONG) moment("-0700-01-01").year(); // 700 (WRONG) moment("-000700-01-01").year(); // -700 (RIGHT)

出于某种原因,使用6位数的年份表示法是可行的.这是正确"的方式吗?为什么像"-700-01-01"这样的符号不起作用?

For some reason a year notation with 6 digits works. Is that the "right" way? Why doesn't notation like "-700-01-01" work?

推荐答案

这不是Moment.js特有的问题;如果您还尝试使用所使用的字符串初始化Date()对象,也会发生相同的情况.如果首先将其创建为Date()对象,然后使用setYear()手动分配年份,则它确实会接受-700的日期:

This isn't a Moment.js-specific problem; the same happens if you attempt to initialise a Date() object with the string you're using as well. If you create it as a Date() object first and manually assign the year using setYear() it does accept a date of -700:

var date = new Date(); date.setYear(-700); moment(date).year();

> -700

但是尼尔斯·库伦杰斯(Niels Keurentjes)指出,很久以前的日期计算变得非常复杂,可能根本不可靠.

However as Niels Keurentjes has pointed out, date calculations this far back get quite complicated and may not be at all reliable.

如果要"-700-01-01",则可以分别配置年,月和日:

If you want "-700-01-01" you can configure the year, month and day separately:

date.setYear(-700); date.setMonth(0); date.setDate(1); console.log(date);

> Fri Jan 01 -700 11:53:57 GMT+0000 (GMT Standard Time)

关于公元前700年第一个月的第一天实际上是否是星期五……您必须亲自检查一下.

As to whether the 1st day of the 1st month in 700BC was actually a Friday... you'll have to look that one up yourself.

更多推荐

如何格式化BC日期(例如"

本文发布于:2023-10-20 03:18:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   BC   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!