dayjs().set({
hour: 12,
minute: 30,
second: 30
})
not working...
Update: ObjectSupport plugin https://day.js.org/docs/en/plugin/object-support
please use .set(hour).set(minute).set(second) instead
please use
.set(hour).set(minute).set(second)instead
thanks.
but it is more convenient if support object param, because momentjs support it, when move from momentjs, it doesn't need to change the code.
ok we will
plus one on this feature. It's common for Calendar Dates to be represented in Objects via:
{
year: 2019,
month: 10, //0-11
day: 30
}
momentjs accepts this format during initialization. Would be nice if dayjs had similar capabilities
Especially since .toObject() method exist, would be nice to have the way back of it
这么做感觉挺奇怪的。
const toDayjs = (obj: any) => {
const { years, months, date, hours, minutes, seconds } = obj;
return dayjs().set('year', years).set('month', months).set('date', date).set('hour', hours).set('minute', minutes).set('second', seconds)
}
toObject 的参数字段和设置的字段也不一样。
moment(moment().toObject())
We just released the ObjectSupport plugin to support this. https://day.js.org/docs/en/plugin/object-support
cool!
@iamkun Hello,
Is it correct behaviour that ObjectSupport plugin get month started from 1, but dayjs return month started with 0?
dayjs().get('month') // 8
dayjs({ month: dayjs().get('month') }).get('month') // 7
It's very uncomfortable. I always need to remember that this parts of one lib works in different way.
The expected behaviour something like this:
dayjs().get('month') === dayjs({ month: dayjs().get('month') }).get('month') // true
@xSorc this is absolutely a bug that I did not review it deeply.
dayjs({month: 1})
should return 2020-02-01 instead if 1970-01-01
@xSorc a fixed pr here https://github.com/iamkun/dayjs/pull/1089
fix: fix objectSupport plugin to get the correct result (zero-based month) is released in 1.9.1
Most helpful comment
We just released the
ObjectSupportplugin to support this. https://day.js.org/docs/en/plugin/object-support