Format date and Subtract days using Moment.js - javascript

I would like a variable to hold yesterday's date in the format DD-MM-YYYY using Moment.js. So if today is 15-04-2015, I would like to subtract a day and have 14-4-2015.
I've tried a few combinations like this:
startdate = moment().format('DD-MM-YYYY');
startdate.subtract(1, 'd');
and this:
startdate = moment().format('DD-MM-YYYY').subtract(1, 'd');
and also this:
startdate = moment();
startdate.subtract(1, 'd');
startdate.format('DD-MM-YYYY')
But I'm not getting it...

You have multiple oddities happening. The first has been edited in your post, but it had to do with the order that the methods were being called.
.format returns a string. String does not have a subtract method.
The second issue is that you are subtracting the day, but not actually saving that as a variable.
Your code, then, should look like:
var startdate = moment();
startdate = startdate.subtract(1, "days");
startdate = startdate.format("DD-MM-YYYY");
However, you can chain this together; this would look like:
var startdate = moment().subtract(1, "days").format("DD-MM-YYYY");
The difference is that we're setting startdate to the changes that you're doing on startdate, because moment is destructive.

var date = new Date();
var targetDate = moment(date).subtract(1, 'day').toDate(); // date object
Now, you can format how you wanna see this date or you can compare this date with another etc.
toDate() function is the point.

startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');

Try this:
var duration = moment.duration({'days' : 1});
moment().subtract(duration).format('DD-MM-YYYY');
This will give you 14-04-2015 - today is 15-04-2015
Alternatively if your momentjs version is less than 2.8.0, you can use:
startdate = moment().subtract('days', 1).format('DD-MM-YYYY');
Instead of this:
startdate = moment().subtract(1, 'days').format('DD-MM-YYYY');

startdate = moment().subtract(1, 'days').startOf('day')

In angularjs moment="^1.3.0"
moment('15-01-1979', 'DD-MM-YYYY').subtract(1,'days').format(); //14-01-1979
or
moment('15-01-1979', 'DD-MM-YYYY').add(1,'days').format(); //16-01-1979
``

I think you have got it in that last attempt, you just need to grab the string.. in Chrome's console..
startdate = moment();
startdate.subtract(1, 'd');
startdate.format('DD-MM-YYYY');
"14-04-2015"
startdate = moment();
startdate.subtract(1, 'd');
myString = startdate.format('DD-MM-YYYY');
"14-04-2015"
myString
"14-04-2015"

Related

Get month name of last month using moment

I use the following code to get startDate and endDate of the last months.
// Previous month
var startDateMonthMinusOne = moment().subtract(1, "month").startOf("month").unix();
var endDateMonthMinusOne = moment().subtract(1, "month").endOf("month").unix();
// Previous month - 1
var startDateMonthMinusOne = moment().subtract(2, "month").startOf("month").unix();
var endDateMonthMinusOne = moment().subtract(2, "month").endOf("month").unix();
How can i do to get also the month name ? (January, February, ...)
Instead of unix() use the format() function to format the datetime using the MMMM format specifier for the month name.
var monthMinusOneName = moment().subtract(1, "month").startOf("month").format('MMMM');
See the chapter Display / Format in the documentation
You can simply use format('MMMM').
Here a working example:
var currMonthName = moment().format('MMMM');
var prevMonthName = moment().subtract(1, "month").format('MMMM');
console.log(currMonthName);
console.log(prevMonthName);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

how to get day format from new Date in Angularjs

i want to get the value of the day format from new Date()(current date) in my angularjs projet. I try this code in my javascript file:
var today = (new Date()).toISOString();
console.log(today.getDay());
when running my code, i get this message error :
TypeError: today.getDay is not a function
however there are many solutions with this syntax.
How can i fix it please. Any help is appreciated
Use getDay on the Date object not on the ISO string:
var today = (new Date()).getDay();
getDay returns a value from 0(Sunday) to 6(Saturday).
If you want current date and day according to your timezone then ->
var today = new Date().getDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getDate()
If you want current date and day according to UTC timezone then ->
var today = new Date().getUTCDay() // 0(Sunday) to 6(Saturday).
var currentDate = new Date().getUTCDate()
You can get date by using below code
let dayno = new Date(this.date.getFullYear(), this.date.getMonth() ,20).getDay();<br>
if date is 20-11-2019 then Day No is :3

moment.js - isBetween() method doesn't work consistently

I have a moment.js code that works well:
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(moment(), moment().add(30, 'days'));
// returns true, that's great!
But when I start refactoring to make it more readable it fails to work:
var today = moment();
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(today, today.add(30, 'days'));
// returns false.. but why?
var today = moment();
var startDate = '2015-05-06T19:00:00+0300';
moment(startDate).isBetween(today, moment(today).add(30, 'days'));
You are passing a reference which you have edited by adding 30 days.

momentJS date string add 5 days

i have a start date string "20.03.2014" and i want to add 5 days to this with moment.js but i don't get the new date "25.03.2014" in the alert window.
here my javascript Code:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add("DD-MM-YYYY", 5);
alert(new_date);
here my jsfiddle: http://jsfiddle.net/jbgUt/1/
How can i solve this ?
I like this string format "25.03.2014"
Hope someone can help me.
UPDATED: January 19, 2016
As of moment 2.8.4 - use .add(5, 'd') (or .add(5, 'days')) instead of .add('d', 5)
var new_date = moment(startdate, "DD-MM-YYYY").add(5, 'days');
Thanks #Bala for the information.
UPDATED: March 21, 2014
This is what you'd have to do to get that format.
Here's an updated fiddle
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
var day = new_date.format('DD');
var month = new_date.format('MM');
var year = new_date.format('YYYY');
alert(day + '.' + month + '.' + year);
ORIGINAL: March 20, 2014
You're not telling it how/what unit to add. Use -
var new_date = moment(startdate, "DD-MM-YYYY").add('days', 5);
moment(moment('2015/04/09 16:00:00').add(7, 'd').format('YYYY/MM/DD HH:mm:ss'))
has to format and then convert to moment again.
The function add() returns the old date, but changes the original date :)
startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
new_date.add(5, 'days');
alert(new_date);
You can add days in different formats:
// Normal adding
moment().add(7, 'days');
// Short Hand
moment().add(7, 'd');
// Literal Object
moment().add({days:7, months:1});
See more about it on Moment.js docs: https://momentjs.com/docs/#/manipulating/add/
var end_date = moment(start_date).clone().add(5, 'days');
If we want to use the current date or present date:
var new_date = moment(moment(), "MM-DD-YYYY").add(7, 'days')
alert(new_date);
To get an actual working example going that returns what one would expect:
var startdate = "20.03.2014";
var new_date = moment(startdate, "DD.MM.YYYY");
var thing = new_date.add(5, 'days').format('DD/MM/YYYY');
window.console.log(thing)
add https://momentjs.com/downloads/moment-with-locales.js to your html page
var todayDate = moment().format('DD-MM-YYYY');//to get today date 06/03/2018 if you want to add extra day to your current date
then
var dueDate = moment().add(15,'days').format('DD-MM-YYYY')// to add 15 days to current date..
point 2 and 3 are using in your jquery code...
If you do end up running with formatting problems after adding X time to the function, try this format:
startDate = moment(startDate).add(1, "days").format("YYYY-MM-DD");
instead of:
startDate = moment(startDate, "YYYY-MM-DD").add(1, "days");
This last version keeps the time attached to the returned data, whereas the format method doesn't and literally returns YYYY-MM-DD.
You can reduce what they said in a few lines of code:
var nowPlusOneDay = moment().add('days', 1);
var nowPlusOneDayStr = nowPlusOneDay.format('YYYY-MM-DD');
alert('nowPlusOneDay Without Format(Unix Date):'+nowPlusOneDay);
alert('nowPlusOneDay Formatted(String):'+nowPlusOneDayStr);
updated:
startdate = "20.03.2014";
var new_date = moment(startdate, "DD-MM-YYYY").add(5,'days');
alert(new_date)

Parsing a date in JavaScript?

Is there a way I could get the year, month (0 based) and day from '03/05/2013'
If so, how?
Thanks
Is there a safe way to do it that can check if it is in the correct format?
You have the Date.parse method which parses a Date string and returns its timestamp, so you can call new Date().
Something like this:
new Date(Date.parse('03/06/2013'))
Most easy is using the split() function, i think:
var date = "03/05/2013";
var dateParts = date.split("/");
var day = dateParts[0];
var month = dateParts[1];
var year = dateParts[2];
http://jsfiddle.net/s7ma2/1/

Categories