Open In App

Node.js date-and-time Date.transform() Method

The date-and-time.Date.transform() method is used to transform date and time from the existing string format to new string format.

Required Module: Install the module by npm or used it locally.



npm install date-and-time --save
<script src="/path/to/date-and-time.min.js"></script>

Syntax:

const transform(dateString, arg1, arg2[, utc])

Parameters: This API takes the following arguments as parameters.



Return Value: This method returns a formatter string.

Example 1:




// Node.js program to demonstrate the  
// Date.transform() method
  
// Importing http module
const date = require('date-and-time')
  
// Creating object of current date and time 
// by using Date() 
const now  =  new Date(07-03-2021);
  
// Changing the format of date and time
// by using date.transform() api
const value = date.transform(
    now.toLocaleDateString(),
    'D/M/YYYY', 'YYYY/M/D');
  
// Display the result
console.log("transformed date and time :- "
     + value)

Output:

transformed date and time :- 1970/1/1

Example 2:




// Node.js program to demonstrate the  
// Date.transform() method
  
// Importing http module
const date = require('date-and-time')
  
// Changing the format of date and time
// by using date.transform() api
const value = date.transform(
    '23:14:05 GMT+0900',
    'HH:mm:ss [GMT]Z', 'YYYY/M/D');
  
// Display the result
console.log("transformed date and time :- "
      + value)

Output:

transformed date and time :- 1970/1/1

Reference: https://github.com/knowledgecode/date-and-time#transformdatestring-arg1-arg2-utc


Article Tags :