Open In App

Moment.js MSDate Plugin

Last Updated : 25 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Moment.js MSDate Plugin is an easy-to-use plugin that allows you to parse OLE Automation dates into Moment.js date objects and vice versa. An OLE Automation date is a fractional value that consists of the number of days before or after the midnight of 30 December 1899 and the time of the day divided by 24.

Write the below command in the terminal to install the MSDate Plugin:

npm install moment-msdate

Once the plugin is installed, you can include it in your project by requiring it in your code:

const momentMsDate = require('moment-msdate');

 

The following are some of the methods that are made available by this plugin:

  • toOADate() method: This method converts the given date to OLE Automation Date string.
  • fromOADate() method method: This methods parses the given OLE Automation Date and returns a Moment object. 

The below examples will help to understand some of the methods of the MSDate Plugin.

Examples 1: Using the toOADate() method for converting the given datetime to an OLE Automation date.

Javascript




const momentMsDate = require('moment-msdate');
  
// Using the current date and time
const oaDate =
    momentMsDate().toOADate();
console.log("Converted OA Date 1:", oaDate); 
  
// Using a given date and time
const oaDate2 = 
    momentMsDate("2022-01-02T05:25:47Z").toOADate();
console.log("Converted OA Date 2:", oaDate2);


Output:

Converted OA Date 1: 44928.3054494213
Converted OA Date 2: 44563.22623842592

Example 2: Using the fromOADate() method to get the datetime from an OLE Automation date.

Javascript




const momentMsDate = require('moment-msdate');
  
const date = momentMsDate.fromOADate("44928.289441817135");
console.log("Parsed Moment Date is:", date);


Output:

Parsed Moment Date is: Moment<2023-01-02T06:56:47Z>

Reference: https://momentjs.com/docs/#/plugins/msdate/


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads