How to Remove Time From Date with Moment.js?
Last Updated :
25 Jul, 2024
Moment.js is a widely used JavaScript library for managing and manipulating dates and times. It simplifies complex date operations and makes it easier to work with dates in a consistent format. One common task is to extract just the date portion from a full timestamp, ignoring the time part. This is particularly useful when we want to display dates without considering specific times or when we need to perform operations based only on dates.
To use Moment.js in the project, we need to install it first. Use the below command to install Moment.js.
npm install moment
To remove the time from a date using Moment.js we can use several approaches:
Using startOf() Method
In this approach we will use startOf() method which sets the time to the start of the specified unit (e.g., day, month). By setting the unit to day we can effectively remove the time portion from the date.
Syntax:
moment(date).startOf('day');
Example: In the below example we are using the startOf() method to remove time from the date with Moment.js.
JavaScript
const moment = require('moment');
let date = moment('2024-07-24T15:30:00');
let dateWithoutTime = date.startOf('day');
console.log(dateWithoutTime.format('YYYY-MM-DD'));
Output:
2024-07-24
In this approach we will use the format() method it allows us to specify a format string to display the date. By providing a format that excludes the time we can effectively ignore the time portion.
Syntax:
moment(date).format('YYYY-MM-DD');
Example: In below example we are using format() method to remove time from date with Moment.js.
JavaScript
const moment = require('moment');
let date = moment('2023-07-13T15:22:10');
let formattedDate = date.format('YYYY-MM-DD');
console.log(formattedDate);
Output:
2023-07-13
Using toDate() Method
In this approach we are using the toDate() method. This method converts a Moment.js object to a native JavaScript Date object. By combining this with standard JavaScript methods we can manipulate and format the date as needed.
Syntax:
moment(date).toDate().toISOString().split('T')[0];
Example: In below example we are using toDate() method to remove time from date with Moment.js.
JavaScript
const moment = require('moment');
let date = moment('2024-07-24T15:30:00');
let dateWithoutTime = date.toDate().toISOString().split('T')[0];
console.log(dateWithoutTime);
Output:
2024-07-24
Similar Reads
How To Set Time With Date in Moment.js? Moment.js is a powerful JavaScript library that makes it easy to work with dates and times. Sometimes, we want to set a specific time to an already existing date. For example- we have a date like "2024-08-15" and we want to set the time to "9:00 AM". Moment.js provides simple ways to do this. In thi
2 min read
How to Format Date with Moment.js? Formatting dates is essential for presenting date and time information in a way that's readable and useful for users. Moment.js provides several methods for formatting dates from simple and predefined formats to more complex and localized representations.Below are different approaches:Table of Conte
2 min read
How to remove time from date using JavaScript ? Given a Date Object and the task is to remove the time portion from the object using JavaScript. JavaScript split() Method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split( separator, limit ) Parameters: separator: This parameter is
2 min read
How to remove time from date using JavaScript ? Given a Date Object and the task is to remove the time portion from the object using JavaScript. JavaScript split() Method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split( separator, limit ) Parameters: separator: This parameter is
2 min read
How to remove time from date using JavaScript ? Given a Date Object and the task is to remove the time portion from the object using JavaScript. JavaScript split() Method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split( separator, limit ) Parameters: separator: This parameter is
2 min read
How To Extract time From MomentJS Object? MomentJS is a powerful library for handling dates and times in JavaScript. It's widely used for formatting, manipulating, and parsing dates. Often, while working with MomentJS, you might need to extract just the time part from a MomentJS object. These are the approaches for extracting time from the
2 min read