Pad day and time in MomentJS without extra logic - javascript

I have a date formatted in Moment.JS but when I call
currentDay.month()
//Or
currentDay.date()
I get a 1 digit number (for example Jan 1st would be 0 for month and 1 for day). I need to get this with a leading 0 for date and month if less than 10. I can do this with something like...
currentDay.date() >= 10 ? currentDay.date() : '0'+currentDay.date()
But I was looking for something a bit nicer. Can I do this easily with MomentJS? I don't see anything in docs.

You can do this, eg for the month, with
moment().format('MM')

Related

Moment js: format date without zero padding

How can I format a date with momentjs without leading zeros? E.g. 2018-6-29 instead of 2018-06-29.
If the number is smaller than 10 it should not add 0 before it.
According to the docs (https://momentjs.com/docs/#/parsing/string-format/) if you use M or D instead of MM or DD in the format() function you will get the date without the 0.
moment().format("YYYY-M-DD") is what you are after.
if you also want to exclude the 0 from single digit days you can use:
moment().format("YYYY-M-D")
(fiddle here: http://jsfiddle.net/rLjQx/69671/)
You should use the format string YYYY-M-D.
Consult the documentation of how to format moments. https://momentjs.com/docs/#/displaying/format/

Angular date issue

I have an object with a date property. I set the date to 01 April 2000 and can see in the debugger that it is set properly to the same date. However when I do a getMonth() on the same date object it returns month as 3 ( March ) . Why is this happening. Does it have anything to do with UTC or Localization both of which i am not using ?
You need +1 for getMonth function
var month = date.getMonth() + 1
this is normal behavior. Months starts with Zero(i.e january is 0). thats why its giving 3 for april. use adding 1 with month to get exact month's digit value.
i guess, to help indexing javascript starts month with zero.
suppose an array of months in string form then we dont have to worry to fetch correct month from string.

angularJS display +1 month date

here is code:
date: new Date(2015,12,8)
and html:
<span>{{task.date | date:'MM/dd/yyyy'}}</span>
but angularJS displays this date like
01/08/2016
and I don't know why it's display +1 month
what I missed?
In JavaScript Date object the months are indexed starting with zero - i.e. Januray == 0.
In other words, you may want to change your date creation as follows:
date: new Date(2015,11,8)
please, notice that 11 stands for December
Months are indexed from 0. So your january month is 0 and Feb is 1 and so on. The reason why it starts with zero and not one, may be because it helps with indexing into arrays.

Set a range in moment.js

I would like to know how to set a day range on moment.js, I just need to show just the weekdays not the weekends.
At the moment I'm using this:
Database: moment().subtract('days', 1).toDate(),
{{Database}}
I use this to show the database refresh but on weekends there are no updates.
Based on your comments, I think that this does what you want:
m.subtract(m.day() === 1 ? 3 : 1, 'days');
If m is a moment, this will subtract 1 day except for on Monday (where m.day() === 1) where it will subtract 3 days. This means that m will go through the days backwards like Wednesday, Tuesday, Monday, Friday, Thursday, etc.
If this is what you want, I am happy to help you modify your question to make it more clear. If not, please edit it to explain what you are trying to do.

Day overflows in javascript Date constructor

I am currently writing some sort of javascript based client calendar and observed some issues. All over the net, I can find code samples, where people use day overflows in the Date constructor.
i.e.
// get the first day of the next month
var myDate = new Date(someDate.getFullYear(),someDate.getMonth(),32);
myDate.setDate(1);
The general idea of this concept is, that since there is no month with 32 days, the constructor will create a date within the next month. I saw even codesample with negative overflows:
i.e.
// get the last day of the previous month
var myDate = new Date(someDate.getFullYear(),someDate.getMonth(),1);
myDate.setDate(-1);
Now while this seems to work in many cases, I finally found a contradiction:
// this prints "2012-12-30" expected was "2012-12-31"
var myDate = new Date(2013,0,1);
myDate.setDate(-1);
Further examination finally revealed that dates like
new Date(2013,0,23) or new Date(2013,0,16) combined with setDate(-1) all end up in "2012-12-31". Finally I observed that using -1 seems to subtract two days (for getting the expected result setDate(0) has to be used).
Is this a bug in the browser implementations or are the code samples spread accross the internet crap??
Furthermore, is this setDate with positive and negative overflow secure to be used and uniformly implemented by all major browsers?
From MDN:
If the parameter you specify is outside of the expected range, setDate attempts to update the date information in the Date object accordingly. For example, if you use 0 for dayValue, the date will be set to the last day of the previous month.
It's logical if you think about it: setDate(1) sets the date to the first of the month. To get the last day of the previous month, that is, the day before the first of this month, you subtract one from the argument and get 0. If you subtract two days (1 - 2) you get the second to last day (-1).
are [..] code samples spread accross the internet crap?
Yes. This is true at least 90% of the time.
At MDN they say:
If the parameter you specify is outside of the expected range, setDate
attempts to update the date information in the Date object
accordingly. For example, if you use 0 for dayValue, the date will be
set to the last day of the previous month.
So you're getting coherent results:
1 - Jan 1
0 - Dec 31
-1 - Dec 30
-2 - Dec 29
Edit: It may look counter-intuitive if you think of it as a mere relative value, such as PHP's strtotime() function:
strtotime('-1 day');
It's not the case ;-)

Categories

Resources