Doing some code in JavaScript/jQuery and I need to have it where the user can enter a somewhat partial date string and have JavaScript figure out the full date.
For example, if the user enters “5/24” it should return “5/24/2011” (the current year). If the user enters “5/24/2004” it should return just that.
duedate = Date.parse(qadueparse);
duedatelit = new Date(duedate);
duedatelit = Number(duedatelit.getMonth()+1) + '/' +
Number(duedatelit.getDate()) + '/' + Number(duedatelit.getFullYear());
This above code works when the user supplies a full string, however it seems to screw up on me when the user types “5/24” or some other partial string. It returns a year of 2001.
Can anyone help me out?
Turning once again to StackOverflow for my programming needs; thank you, everyone, in this vibrant community for supporting each other.
The date object can accept a deconstructed date for instantiation:
You can rip apart the partial date and fill in the blanks yourself. However, what is 3/7? March 7th? July 3rd? Worse yet, 3/5/7 – which is year, which is month, which is day?
comment followup:
If it’s always m/d, then you just split on the
/, giving you m and d values.