How to use the setHours function from date-fns

Find comprehensive JavaScript date-fns.setHours code examples handpicked from public code repositorys.

date-fns.setHours is a function that sets the hour value of a Date object to a specified value, returning a new Date object with the updated value.

1
2
3
4
5
6
7
8
9

const {setHours, setMinutes} = require('date-fns');

module.exports = function (date, hours, minutes) {
  const dateWithoutTime = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  const withHours = setHours(dateWithoutTime, parseInt(hours, 10));
  const withHoursAndMinutes = setMinutes(withHours, parseInt(minutes, 10));
  return withHoursAndMinutes.toISOString();
};
fork icon5
star icon10
watch icon4

+ 3 other calls in file

25
26
27
28
29
30
31
32
33
34
if (daysOfWeek) {
  utcDaysOfWeek = {};
  for (const dayOfWeek of DAYS_OF_WEEK) {
    const dayOfWeekIndex = DAYS_OF_WEEK.indexOf(dayOfWeek);
    let date = nowUtc();
    date = setHours(date, timeHours);
    date = setMinutes(date, timeMinutes);
    date = setDay(date, dayOfWeekIndex);
    const utcDate = zonedTimeToUtc(date, timezone);
    const utcDayOfWeekIndex = utcDate.getDay();
fork icon6
star icon8
watch icon0

How does date-fns.setHours work?

date-fns.setHours works by taking two inputs: a Date object and a numeric value between 0 and 23 representing the desired hour.

The function returns a new Date object with the hour value set to the specified value.

If the specified hour value is out of range, the function will adjust the other fields of the Date object as necessary to maintain the correct date and time.

For example, calling setHours(new Date(), 25) would set the hour value to 1 and adjust the date and time fields accordingly.

By using date-fns.setHours, you can modify the hour value of a Date object without modifying the original object, allowing you to work with immutable dates and times.

170
171
172
173
174
175
176
177
178
179
    date.setHours(date.getHours() + hours);
  
    return date;
  }
const newDate = addHours(now, 8);
const noon = setHours(now, 12);

// const timeOut = format(newDate, 'HH:mm');
const timeOut = format(newDate, 'HH:mm');
const timeFormat = 'HH:mm';
fork icon0
star icon3
watch icon1

+ 7 other calls in file

145
146
147
148
149
150
151
152
153
154
}
exports.parseImpl = dateFns.parse
exports.setDateImpl = dateFns.setDate
exports.setDayImpl = dateFns.setDay
exports.setDayOfYearImpl = dateFns.setDayOfYear
exports.setHoursImpl = dateFns.setHours
exports.setISODayImpl = dateFns.setISODay
exports.setISOWeekImpl = dateFns.setISOWeek
exports.setISOYearImpl = dateFns.setISOYear
exports.setMillisecondsImpl = dateFns.setMilliseconds
fork icon0
star icon2
watch icon1

+ 16 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
const { setHours } = require("date-fns");

// Create a new Date object representing the current date and time
const date = new Date();

// Set the hour value of the Date object to 12
const newDate = setHours(date, 12);

console.log(date.getHours()); // The original hour value
console.log(newDate.getHours()); // The new hour value

In this example, we use the date-fns module to set the hour value of a Date object to 12. We first create a new Date object representing the current date and time. We then use setHours to create a new Date object with the hour value set to 12. We compare the original and new hour values using the getHours method of the Date object. The resulting output demonstrates how date-fns.setHours can be used to modify the hour value of a Date object and create a new object without modifying the original object.

-1
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Other functions in date-fns

Sorted by popularity

function icon

date-fns.format is the most popular function in date-fns (3838 examples)