Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dayjs().set() support Object param? #677

Closed
sam019 opened this issue Sep 17, 2019 · 14 comments
Closed

dayjs().set() support Object param? #677

sam019 opened this issue Sep 17, 2019 · 14 comments

Comments

@sam019
Copy link

sam019 commented Sep 17, 2019

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

not working...

@iamkun
Copy link
Owner

iamkun commented Sep 17, 2019

Update: ObjectSupport plugin https://day.js.org/docs/en/plugin/object-support


please use .set(hour).set(minute).set(second) instead

@sam019
Copy link
Author

sam019 commented Sep 17, 2019

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.

@iamkun
Copy link
Owner

iamkun commented Sep 17, 2019

ok we will

@ChuckJonas
Copy link

ChuckJonas commented Oct 19, 2019

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

@Epistol
Copy link

Epistol commented Feb 4, 2020

Especially since .toObject() method exist, would be nice to have the way back of it

@xiaohuoni
Copy link

xiaohuoni commented May 26, 2020

这么做感觉挺奇怪的。

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())

@iamkun
Copy link
Owner

iamkun commented May 26, 2020

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

@xiaohuoni
Copy link

cool!

@iamkun iamkun closed this as completed May 26, 2020
@edikdeisling
Copy link

@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

@iamkun
Copy link
Owner

iamkun commented Sep 28, 2020

@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

@iamkun
Copy link
Owner

iamkun commented Sep 28, 2020

@xSorc a fixed pr here #1089

@iamkun
Copy link
Owner

iamkun commented Sep 28, 2020

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

@BigBird15
Copy link

hi!

i used objectSupport plugin, but for some reason it still doesn'w work :c

dayjs.extend(objectSupport);
dayjs({}); 

it doesn't compile:

TS2769: No overload matches this call.   Overload 1 of 3, '(date?: string | number | Dayjs | Date): Dayjs', gave the following error.     Argument of type '{}' is not assignable to parameter of type 'string | number | Dayjs | Date'.       Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.   Overload 2 of 3, '(date?: string | number | Dayjs | Date, format?: OptionType, strict?: boolean): Dayjs', gave the following error.     Argument of type '{}' is not assignable to parameter of type 'string | number | Dayjs | Date'.       Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.   Overload 3 of 3, '(date?: string | number | Dayjs | Date, format?: OptionType, locale?: string, strict?: boolean): Dayjs', gave the following error.     Argument of type '{}' is not assignable to parameter of type 'string | number | Dayjs | Date'.       Type '{}' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 37 more.

I used other plugins and didn't have any problems with them
what should I do to fix this?

@braydoncoyer
Copy link

I seem to be getting the same issue as @BigBird15, but when passing the object with year, month, and day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants