index.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import toDate from "../toDate/index.js";
  2. import setMonth from "../setMonth/index.js";
  3. import toInteger from "../_lib/toInteger/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.js";
  5. /**
  6. * @name set
  7. * @category Common Helpers
  8. * @summary Set date values to a given date.
  9. *
  10. * @description
  11. * Set date values to a given date.
  12. *
  13. * Sets time values to date from object `values`.
  14. * A value is not set if it is undefined or null or doesn't exist in `values`.
  15. *
  16. * Note about bundle size: `set` does not internally use `setX` functions from date-fns but instead opts
  17. * to use native `Date#setX` methods. If you use this function, you may not want to include the
  18. * other `setX` functions that date-fns provides if you are concerned about the bundle size.
  19. *
  20. * @param {Date|Number} date - the date to be changed
  21. * @param {Object} values - an object with options
  22. * @param {Number} [values.year] - the number of years to be set
  23. * @param {Number} [values.month] - the number of months to be set
  24. * @param {Number} [values.date] - the number of days to be set
  25. * @param {Number} [values.hours] - the number of hours to be set
  26. * @param {Number} [values.minutes] - the number of minutes to be set
  27. * @param {Number} [values.seconds] - the number of seconds to be set
  28. * @param {Number} [values.milliseconds] - the number of milliseconds to be set
  29. * @returns {Date} the new date with options set
  30. * @throws {TypeError} 2 arguments required
  31. * @throws {RangeError} `values` must be an object
  32. *
  33. * @example
  34. * // Transform 1 September 2014 into 20 October 2015 in a single line:
  35. * var result = set(new Date(2014, 8, 20), { year: 2015, month: 9, date: 20 })
  36. * //=> Tue Oct 20 2015 00:00:00
  37. *
  38. * @example
  39. * // Set 12 PM to 1 September 2014 01:23:45 to 1 September 2014 12:00:00:
  40. * var result = set(new Date(2014, 8, 1, 1, 23, 45), { hours: 12 })
  41. * //=> Mon Sep 01 2014 12:23:45
  42. */
  43. export default function set(dirtyDate, values) {
  44. requiredArgs(2, arguments);
  45. if (typeof values !== 'object' || values === null) {
  46. throw new RangeError('values parameter must be an object');
  47. }
  48. var date = toDate(dirtyDate); // Check if date is Invalid Date because Date.prototype.setFullYear ignores the value of Invalid Date
  49. if (isNaN(date.getTime())) {
  50. return new Date(NaN);
  51. }
  52. if (values.year != null) {
  53. date.setFullYear(values.year);
  54. }
  55. if (values.month != null) {
  56. date = setMonth(date, values.month);
  57. }
  58. if (values.date != null) {
  59. date.setDate(toInteger(values.date));
  60. }
  61. if (values.hours != null) {
  62. date.setHours(toInteger(values.hours));
  63. }
  64. if (values.minutes != null) {
  65. date.setMinutes(toInteger(values.minutes));
  66. }
  67. if (values.seconds != null) {
  68. date.setSeconds(toInteger(values.seconds));
  69. }
  70. if (values.milliseconds != null) {
  71. date.setMilliseconds(toInteger(values.milliseconds));
  72. }
  73. return date;
  74. }