Open In App

Moment.js Java DateFormat Parser Plugin

Last Updated : 26 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Moment.js is a JavaScript library that helps parse, validate, manipulate, and display date/time in JavaScript in a very easy way. It also allows displaying of dates as per localization and in a human-readable format.

Java DateFormat Parser Plugin: The Java DateFormat Parser Plugin is used to translate any date provided by the user into the format specified by the end user. This is useful if one needs to seamlessly integrate with java.text.DateFormat of Java.

We will be using the following two methods:

  • moment(): This method accept date or time in string format i.e. moment(“2022-08-15”).
  • formatWithJDF(): This method will be called on the moment object with a single parameter in string format which specifies in what format you need the “date” you provided at the moment().

Syntax: 

moment(YYYY-MM-DD HH:MM:SS)

formatWithJDF(): You can specify in which format you want to translate the date. I want in “dd-mm-yyyy” this format.

formatWithJDF("dd-mm-yyyy")

Return Value: It returns the date in the format provided in formatWithJDF().

Setting up Node.js application: Follow the below steps to set up the application:

Step 1: Create a Node.js application using the following command:

npm init

Step 2: After creating the NodeJS application, Install the required module using the following command:

npm install moment
npm install moment-jdateformatparser

Project Structure: The project structure will look like this:

 

Example 1: In this example, we will pass the date in a specific format to moment() and store its result in a variable called momentDate. On reference variable momentDate we will call the formateWithJDF() method with a single parameter specifying the format of the date we expect this method to return.

  • app.js: Write down the below code in the app.js file:

Javascript




const moment = require('moment');
const javadatefp = require('moment-jdateformatparser');
  
const momentDate = moment("2022-08-15")
const result = momentDate.formatWithJDF("dd/MM/yyyy")
console.log(result)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

15/08/2022

Example 2: In this example, we will pass date and time in a specific format to moment() and store its result in a variable called momentDateAndTime. On reference variable momentDateAndTime we will call the formateWithJDF() method with a single parameter specifying the format of date and time we expect this method to return.

  • app.js: Write down the below code in the app.js file:

Javascript




const moment = require('moment');
const javadatefp = require('moment-jdateformatparser');
  
const momentDateAndTime = moment("2022-08-15 15:50")
const result = momentDateAndTime
    .formatWithJDF("YYYY.MM.DD HH:MM")
console.log(result)


Steps to run the program: To run the application execute the below command from the root directory of the project:

node app.js

Output:

2022.08.15 15:08

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads