dayjs().set() support Object param?

Created on 17 Sep 2019  ·  12Comments  ·  Source: iamkun/dayjs

dayjs().set({
  hour: 12,
  minute: 30,
  second: 30
})

not working...

Most helpful comment

We just released the ObjectSupport plugin to support this. https://day.js.org/docs/en/plugin/object-support

All 12 comments

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

fix: fix objectSupport plugin to get the correct result (zero-based month) is released in 1.9.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oliv9286 picture oliv9286  ·  4Comments

Newbie012 picture Newbie012  ·  4Comments

sohailalam2 picture sohailalam2  ·  4Comments

LauRocky picture LauRocky  ·  5Comments

fes300 picture fes300  ·  5Comments