index.js 2.9 KB

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