我试图找出设置在天。为此,我测试了以下代码:
let orwell = dayjs();
orwell.year(1984);
console.log(orwell.format("YYYY"));
我的期望是1984年将出现在主机上。但事实并非如此,而是2021年出现了。
我做错了什么?
Dayjs日期与时刻日期不同不可变. 这意味着任何一个日期的实例都不能被改变。year setter(或任何类似的方法)不会修改正在调用它的实例,而是返回一个新的dayjs对象。
试试这个:
let now = dayjs();
let orwell = now.year(1984);
console.log(orwell.format("YYYY")); // should print 1984
console.log(now.format("YYYY")); // should print the current year
我不认为我可以改变'dayjs'的对象。
但是,很容易将值作为字符串导入'dayjs()'函数。
useEffect(() => {
let orwell = dayjs('1984').format('YYYY-MM-DD HH:mm');
console.log('!!', orwell);
}, []);