iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
46.76k stars 2.29k forks source link

(feat) Date set time from another date. #1396

Open y0nd0 opened 3 years ago

y0nd0 commented 3 years ago

Feature Description It would be great if there is a method to set the time from another date.

dayjs('2021-03-01T12:34:56.123Z').setTimeFrom(
  dayjs('2021-02-28T10:05:12.870Z')
);

// Result: '2021-03-01T10:05:12.870Z'

I know there are alternatives and I could write my own method. This is just a suggestion. Use case: Merge two dates which comes from separat inputs date and time.

day.js 1.10.4

iamkun commented 3 years ago

Sorry, we have no plan to support this ATM.

codyfyi commented 11 months ago

I came across this need when working with raw iCalendar data. I'd have liked to do something like:

const merged = day1.time(day2.get('time') // use day1's date, with day2's time

This can be done through string manipulation .split('T') to construct the new date or by manually setting hour/minute/second/millisecond on the intended dayjs object from another dayjs object. This feat would be a slight convenience to those options.