How to use the set function from date-fns

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

date-fns.set is a function that returns a new Date object with a specified date or time value changed to a new value.

46
47
48
49
50
51
52
53
54
55
} = payload;

let final = date || new Date();

if (set) {
  final = datefns.set(final, set);
}

if (diff) {
  final = datefns.add(final, diff); // date-fns.add() supports negative numbers as well
fork icon28
star icon313
watch icon7

+ 3 other calls in file

403
404
405
406
407
408
409
410
411
412

const contract = await Contract.create(
  {
    ...data,
    customerId: adherer.id,
    due: set(addMonths(new Date(data.createdAt), paymentMethod.numberInstallments), {
      date: paymentMethod.dueDay,
      hours: 0,
      minutes: 0,
      seconds: 0,
fork icon0
star icon0
watch icon1

+ 3 other calls in file

How does date-fns.set work?

date-fns.set works by taking two or more inputs: a Date object representing the original date, a unit of time to change (such as "year" or "hour"), and a new value for that unit of time.

The function returns a new Date object that represents the same date as the original object, but with the specified unit of time changed to the new value.

For example, if the unit of time is "year" and the new value is 2022, the function returns a new Date object with the year changed to 2022, while keeping the original month, day, and time values.

The resulting Date object can be used for further calculations or manipulations of the date and time values.

By changing individual units of time within a Date object, you can create new Date objects that represent different dates and times with a high degree of precision.

41
42
43
44
45
46
47
48
49
50
51
52
const chegadaDoProduto = differenceInDays(resultado, new Date(2023, 0, 9));


console.log(`Previsão de entrega: ${chegadaDoProduto} dias.`);


//set
const metSet = set(new Date(2023, 0, 2), {
    date: 10
});
console.log(metSet);

fork icon0
star icon0
watch icon0

+ 4 other calls in file

49
50
51
52
53
54
55
56
57
58
59


//Método set
//Existem vários, com funcionamento parecido
//Modifica uma data
//Transforma 1/9/214 para 20/10/2015
const setDate = set(new Date(2014, 8, 20), {
    year: 2015,
    month: 9,
    date: 20
});
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const { set } = require("date-fns");

// A Date object representing January 1, 2023 at 12:00 PM
const date = new Date("2023-01-01T12:00:00");

// Set the year to 2022
const newDate1 = set(date, { year: 2022 });

console.log(newDate1); // 2022-01-01T12:00:00.000Z

// Set the hour to 3 PM
const newDate2 = set(date, { hours: 15 });

console.log(newDate2); // 2023-01-01T15:00:00.000Z

In this example, we use the date-fns.set function to change the year and hour values of a Date object representing January 1, 2023 at 12:00 PM. In the first example, we call set(date, { year: 2022 }) to create a new Date object with the year changed to 2022, while keeping the original month, day, and time values. In the second example, we call set(date, { hours: 15 }) to create a new Date object with the hour changed to 3 PM, while keeping the original year, month, and day values. The resulting Date objects are stored in the variables newDate1 and newDate2, respectively, and are printed to the console.

122
123
124
125
126
127
128
129
130
131
const now = new Date();

events.forEach(async (item) => {
  if (item.days.includes(format(now, "EEEEEE"))) {
    const nowZonedTime = utcToZonedTime(now, timeZone);
    const targetTime = set(nowZonedTime, item.time);
    const minutes = differenceInMinutes(targetTime, nowZonedTime, { roundingMethod: "ceil" });
    if (minutes == interval) {
      const targetUTC = zonedTimeToUtc(targetTime, timeZone);
      const unixTime = getUnixTime(targetUTC);
fork icon0
star icon0
watch icon0

Other functions in date-fns

Sorted by popularity

function icon

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