moment js date library, formatting on IE gives a NaN - javascript

im using moment js date library to format a date, but on IE
i get a NaN on the output. It works fine on other browsers, like Chrome, FF, etc.
var value = "2015-11";
moment(value).format("YYYY-DD-01 00:00")
> "0NaN-NaN-01 00:00"
I was able to fix it by adding the same pattern on moment constructor like below:
> moment(value,"YYYY-DD-01 00:00").format("YYYY-DD-01 00:00")
"2015-11-01 00:00"
Is it a good practice to add this pattern on the constructor, for all moment objects creation
so it can work also on IE?

The input format should match what you are providing:
var value = "2015-11";
moment(value, "YYYY-MM")
If you want to format it differently for output, that's when you use the .format method.
var value = "2015-11";
var m = moment(value, "YYYY-MM")
var s = m.format("YYYY-MM-DD HH:MM")
Note that you were specifying DD which is the day formatter. But based on the usage, I think you meant MM for month.

Related

datejs overwriting Date in javascript

I am using date js to quickly parse any string into a date and it is working perfectly. However I need to also parse a timestamp.
var temp_string = "1484120122526";
var date = new Date(temp_string);
It gives back
NaN –
Regular javascript Date object does this, but I can't find a way for datejs to do it. And since it overwrites the Date object, I am stuck. Can datejs parse timestamps? or is there a way for me to call new Date() and reference the original date object?
Even though dateJS does indeed overwrite the default js Date class, the fault was on my side, I did not see in the documentation the fact that timestamp has to be an int, as I was looking everywhere in the dateJS documentation not the Date one.
so the code should be:
var temp_int = 1484120122526;
var date = new Date(temp_int);

Can't Compare dates using jQuery UI's datepicker

Ok, so I am attempting to test if a date is older than today. I am using jQuery UI's Datepicker to parse the date and assign it to a variable:
//Get Date as String
var $strDate = $(".pmt-date").text();
//Parse Date
var $dtDate = $.datepicker.parseDate("mm/dd/yy", $strDate);
Then I get today's date and assign it to a variable:
//Get Today's Date
var $strToday $.datepicker.formatDate('mm/dd/yy', new Date());
var $tDate = $.datepicker.parseDate('mm/dd/yy', $strToday);
Now I would like to compare $dtDate with $tDate. This is what I have tried:
if($dtDate > $tDate)
{
alert("Payment Date is Greater");
}
else
{
alert("Today's Date is Greater");
}
When I test this, I ALWAYS get the alert "Today's Date is Greater". I can display my two date variables via an alert, and I see the dates in correct format. So why does this comparison fail to work when the parse is working correctly?
Assuming that the field with class "pmt-date" is the datepicker-controlled <input> element, you need to fetch its value with .val(), not .text().
var $strDate = $(".pmt-date").val();
Your next line of code refers to a variable called "$date", not "$strDate", so:
var $dtDate = $.datepicker.parseDate("mm/dd/yy", $strDate);
Once you've got that, you can just directly compare the Date objects:
if ($dtDate < new Date())
There's no need to turn a newly-constructed Date object into a string and then back into a date. I guess you're Date to string and back in order to strip off the time-of-day part of the date, so that's not really a bad way to do it.
In date comparisons, more than means the date comes after, and less than means the date comes before. Older than would imply that the date comes before, and thus you want to use less than
if($dtDate < $tDate)

Converting (formatting) 2-digit date to a 4-digit date

I'm having trouble properly formatting 2-digit dates as 4-digit dates.
I have a text input field:
<input type="text" value="" class="date"/>
And then I want to format it in the "mm/dd/yyyy" format after the user enters a date. So I have an onChange event:
$('.date').on('change',function(){
var date = new Date($(this).val());
$(this).val((date.getMonth()+1)+'/'+date.getDate()+'/'+date.getFullYear());
});
Okay, so now comes the oddities (I'll be using 12/12/12 as an example date):
1) getFullYear() returns 1912 in all IE, FF. Chrome returns 2012.
2) getYear() returns 112 in IE, Chrome. FF returns 12.
So it seems like the only option at this point is to sniff the user agent, and use that accordingly. Is there anyway around sniffing the UA?
Using moment.js:
const moment = require("moment")
const date = moment("21", "YY")
const year = date.format("YYYY")
you can try,first, to seperate the parts of the input
...
var input=$(this).val();
var month=input.substr(0,2)-1;
var day=input.substr(3,2);
var year=parseInt("20"+ input.substr(6,2));
and then initate a new Date object:
var date= new Date(year,month,day);
or write it back to the field, as you did:
$(this).val((date.getMonth()+1)+'/'+date.getDate()+'/'+date.getFullYear());
After asking around, I ended up using Moment.js. I tried date.js but because I'm partially working with unix timestamps, moment seemed to handle these better (IMO).
const [day, month, year] = values.date.split('/');

Javascript milliseconds notaion was not working in firefox

Can anyone help me with this ?
var current_date=new Date('2012/12/21 22:59:59.997');
var result=current_date.getTime();
Im not getting result in Firefox but it does show in chrome, in FF it shows invalid date.
You should be able to do the following (using date.setMilliseconds):
var dateString = '2012/12/21 22:59:59.997';
var dateStringSplit = dateString.split('.');
var myDate = new Date(dateStringSplit[0]);
myDate.setMilliseconds(dateStringSplit[1]);
console.log(myDate);
Firefox and some other browsers (namely, Safari or Opera) don't like milliseconds.
// Split off the part after the dot
var current_date = new Date('2012/12/21 22:59:59.997'.split('.')[0]);
// Works everywhere!
var result = current_date.getTime();
If you really want to work with milliseconds, you have to split the date in multiple parts and use new Date() with those. From MDN documentation, here are the options:
new Date(year, month, day [, hour, minute, second, millisecond])
Or, as h2ooooooo says, you can use the second part of the split date and use setMilliseconds.
Overall, you have plenty of solutions. Choose the one that annoys you the least.
The format you are using is not a standard format for Date.parse
You may want to split the string and set the parts individually. Also, please manage the time zone correctly as it is not apparent which timezone the date string is in.

javascript : how to get 'utc date'

I have a date in this string format "02/28/2012" and I want to convert it to UTC.
I'm using the jquery datepicker to select thedate and populate an inputbox. any clues?
Thanks
var datestr = "07/08/2005";
var datearr = datestr.split("/")
var utc = Date.UTC(datearr[2],datearr[0],datearr[1]);
var utcdate = Date.UTC(2012,2,28);
The other answers are good, but they will give you the wrong result.
In Javascript, the month argument is zero-indexed, so make sure to subtract 1 from the standard month number,
var utcms = Date.UTC(2012,2-1,28);
Unfortunately jquery .datepicker.parseDate(str) injects a local timezone (it would be nice if the documentation said this), and Date(str) and Date.parse(str) appear unpredictable about their treatment of local vs UTC.

Categories

Resources