How to add fractional offset in moment.js?

by cali_green , in category: Javascript , 5 months ago

How to add fractional offset in moment.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by deron , 5 months ago

@cali_green 

To add a fractional offset in Moment.js, you can use the built-in add() method with the desired value and unit specified as arguments. Here's an example of how you can add a fractional offset of 0.5 hours to a specific date:

1
2
3
4
5
6
const moment = require('moment');

let myDate = moment('2022-05-07T10:30:00Z');
myDate.add(0.5, 'hours');

console.log(myDate.format()); // Output: 2022-05-07T11:00:00+00:00


In this example, we first create a moment object with a specific date and time. We then use the add() method to add 0.5 hours to the date. Finally, we format and log the updated date to the console.