Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract only date from moment object

Tags:

javascript

I am using moment object for date. and for that i wrote

dateOnly = moment(date.format("MM/DD/YYYY"));
var ndate = dateOnly.toDate();

I want to display only date on front end but it is showing me in this format that I don't want

Mon Feb 22 2016 00:00:00 GMT+0530 (India Standard Time)

like image 844
Sayili Arya Avatar asked Feb 22 '16 06:02

Sayili Arya


People also ask

How do you find the date string from the moment?

js parsing date and time. We can parse a string representation of date and time by passing the date and time format to the moment function. const moment = require('moment'); let day = "03/04/2008"; let parsed = moment(day, "DD/MM/YYYY"); console. log(parsed.

How do you use moments to date?

moment(). format('YYYY-MM-DD'); Calling moment() gives us the current date and time, while format() converts it to the specified format. This example formats a date as a four-digit year, followed by a hyphen, followed by a two-digit month, another hyphen, and a two-digit day.

How do you find a date with a moment number?

var check = moment('date/utc format'); day = check. format('dddd') // => ('Monday' , 'Tuesday' ----) month = check. format('MMMM') // => ('January','February.....) year = check. format('YYYY') // => ('2012','2013' ...)


2 Answers

You can use moment's format function to display date in the format you want to

moment().format('MM/DD/YYYY'); // "02/22/2016"

You can go through all the available formats here

http://momentjs.com/docs/#/displaying/

like image 80
satish Avatar answered Oct 01 '22 18:10

satish


var dateOnly = moment(date).format('MM/DD/YYYY'); // for formatted dates.
yesterday= moment(date).subtract(1,'days') // for previous day 
//you can replace days with months and years.
tomorrow= moment(date).add(1,'days') // for upcoming day 
//you can replace days with months and years. 
var date1= moment(yesterday)
var date2= moment(tomorrow)
differencebetweenmoments =moment(date1).diff(moment(date2),'days');
like image 27
Sakif Abu Avatar answered Oct 01 '22 17:10

Sakif Abu