Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make javascript Date.parse understand short years?

I noticed Date.Parse can't handle only 2 digits dates.

Say I have this

mm/dd/yy = 7/11/20

Date parse will think it is = 7/11/1920. Can you set it to use the year two thousand? Like it's kinda weird I got the jquery u.i date picker and if you type in 7/11/20 it will figure out 2020.

So it would be nice if Date.parse could keep up I rather have them both not know what is going on or both know what is going on then have one that knows and one that does not know.

like image 915
chobo2 Avatar asked Jul 12 '10 01:07

chobo2


People also ask

What can I use instead of date parse?

If the DATEPARSE function is not available for the data that you're working with, or the field you are trying to convert is a number data type, you can use the DATE function instead. The DATE function converts a number, string or date expression to a date type.

How do you parse time from a date?

The parse() method takes a date string (such as "2011-10-10T14:48:00" ) and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the setTime() method and the Date object.

How do I get the difference between two dates in JavaScript?

Approach 1: Define two dates using new Date(). Calculate the time difference of two dates using date2.getTime() – date1.getTime(); Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.write().


1 Answers

Not that I'm aware of. But you can always adjust the year:

YourDate="7/11/20";
DateObj=new Date(YourDate.replace(/(\d\d)$/,"20$1"));

alert(DateObj);

This code in action.

Edit: The following code will handle both full and short years:

YourDate="7/11/2020";
DateObj=new Date(YourDate.replace(/\/(\d\d)$/,"/20$1"));

alert(DateObj);

This code in action.

like image 95
Gert Grenander Avatar answered Oct 24 '22 16:10

Gert Grenander