I have the following line of javascript using MomentJS
var date = moment('20/04/20000', 'DD/MM/YYYY'); //notice year twenty thousand
alert(date.format()); // alerts "2000-04-20T00:00:00+01:00"
If I change the format to DD/MM/YYYYY it works as expected, except of course if I enter a 6 digit year. I know this is arbitrary, but it bothers me. How do I use a format that will expect any amount of digits in the year?
It looks like moment ignores any extra format specfiers. So you can just use as many Y's as you expect you'll need for your max date. For example this code tells moment to expect 10 digit years:
moment('20/04/20000', 'DD/MM/YYYYYYYYYY').format();
but it's return value looks like what you're expecting, 5 digit year:
"20000-04-20T00:00:00-04:00"
This is not a documented feature, and I'd be very careful about this code as you use future moment updates. Protect this code with unit tests for sure.
Related
I'm using moment.js to format a datetime value as follows:
time = moment(this.params['timeValue']).format("HHmmss.SSS")
Here, this.params['timeValue'] is a string. An example value is 2020-02-05 10:00:00.123 . formatting using moment returns the value 100000.123. Sometimes my datetime value goes to microsecond value, but moment js seems to cut the formatted value to millisecond level. For example formatting 2020-02-05 10:00:00.123456 returns 100000.123. I've tried this, but it did not work:
time = moment(this.params['timeValue']).format("HHmmss.SSSSSS")
Could you help me how to handle the formatting of a datetime value with microseconds. If moment.js doesn't handle it, is there any other library I could use?
Moment.js wraps the native date type, which goes down to only milliseconds. You will only get the first three digits, the moment.js documentation shows an example of the fractional seconds (as per your second code snippet), but also mentions that it will only display the 3 significant digits, filling the rest with zeros.
Check out this SO question for more information about microseconds in JavaScript.
Let's say I have a field that contains, say, a gestation period in terms of a number of days:
var gestation = 132; // days
Now, let's say, using Moment.js I want to return a value that figures out a semantic expression for that gestation period, depending upon its length. In other words, if the gestation period, is say, 10 days, I want the method to return a value of about a week or 10 days or something similar. If the gestation period is 365 days, I want the method to return a value of a year and so on.
How does one write the Moment.js expression to accomplish this?
This seems similar to the Time to X approach described here in the docs. But not exactly.
One possible solution is to use subtract() combined with to() like in this jsfiddle
Use the humanize() method.
moment.duration(gestation, 'days').humanize();
Found in the documentation here.
I'm using moment-with-locales.min.js for date manipulation and I need to format as the user leaves the textboxes. Because locales are an issue, I'm trying to use moment to do the formatting. However, I'm running into a problem and I'm not sure if I'm doing it wrong or what.
If the user types in something like '2/2/12' and I try to do moment('2/2/12', 'l'), using the 'l' for short date based on locale, it formats it into '2/2/0012'. That in itself seems broken.
If I try to format with moment('2/2/12', 'M/d/yyyy'), as seen in the JSFiddle below, it changes it to '2/1/2014'. It always bumps it down to the first day of the month and makes it the current year.
Here's the JSFiddle I was using.
moment.locale('en-US');
var parsed = moment('2/2/12', 'M/d/yyyy');
if (moment(parsed).isValid()) {
var d = new moment(parsed, 'l');
alert('Pass: ' + d.format('l'));
} else {
alert('Fail: ' + parsed);
}
I'd appreciate and help.
You should use "D" ("d" is day of week).
As for the year, according to the docs, «Two digit year parser by default assumes years above 68 to be in the 1900's and below in the 2000's.» so I'm guessing (and experimentation seems to confirm it) that if you use the 4 digit format ("YYYY") it'll assume you are passing in the year 12 and not 2012. If you use "YY" it'll print 2012 correctly.
So, to summarize, the format "M/D/YY" should do what you want.
Hey guys, Im having trouble knowing wat is the correct date format i should put for date.parse() function.
I want to use mm/dd/yyyy in the function like this.
alert("Date: "+Date.parse("11/28/2011"));
//-->Its showing me Date:NaN as output.
also I wanted to know what output should I be getting if I do this following code:
date = Date.parse('12/31/2011' +' UTC');
var minutes=1000*60;
var hours=minutes*60;
var days=hours*24;
var years=days*365;
var y=date/years;
t should give me exactly 42 years but its giving me decimal as well. Y so??
Any help will be appreciated guys.
Thanks
Anand
International (ISO) standard date time format will always work. 2011-12-31
You may also look at this and this
Why would it give you exactly 42 years? Don't forget that there's no such thing as an exact number of milliseconds in a year, due to leap years (and leap seconds, if they're accounted for). Even if there were, you're getting the start of the last day of 2011, which isn't the same as the start of the first day of 2012, which is probably what you were thinking of.
The exact formats supported are implementation-specific as far as I can tell - at least for ECMAScript (which I realise isn't quite the same as JavaScript).
I need a way to turn my 2 character string dates (i.e. '04/10/2010' & '05/24/2010') into an integers to see if one is greater than the other. If the user enters an end date that is less than the begin date I need to popup an "invalid date range" error.
From what you posted your real problem is:
I need to see if one date is greater than another in javascript/jquery.
If so all you need to use is the Javascript Date object (how to page here).
You can use it as follows:
var dateTextA = '04/10/2010';
var dateTextB = '05/24/2010';
var dateA = new Date(dateTextA);
var dateB = new Date(dateTextB);
if (dateA < dateB){
alert("Your date is out of range!");
}
Note: Above code has been tested and works in IE7.
If you really feel you need an integer value, you can use the UTC function to get that.
The problem is Date.parse('04/10/2010') returns a timestamp (integer) for April 10 in the US and 4 October most other places.
It is best to use a less ambiguous format - if you are taking user input, give the user a calendar, menu, or three label inputs, then build the date from that.
3 inputs:
new Date(+fullyearinput, monthinput-1, +dateinput)
If Date won't parse what you are looking for, Datejs provides a lot of syntactic sugar to make your life easier.
To compare two dates, all you need to do is turn your strings into Date objects and then use the greater than, less than, or equality operators. Javascript provides Date comparison natively.