Working late last night in Alva I wanted to do something that sounded trivial:
When the page loads, get the current date and time, and if a certain input is empty, put it there like this:
So, how hard can that be, right? Well not hard, but...
To get the elements I want to put in the value, I used accessor methods. So, obviously, these should give me what I want for the string, right?
now.getDay(), now.getMonth(), now.getYear(), now.getHour() now.getMinute()
Well, they are, at the date mentioned above, respectively: 2, 4, 113, error, error
Ok, the errors are easy to fix from the docs. It's actually getHours() and getMinutes(), so now we have 2, 4, 113, 23, 45 and of those five things, the last two are what one would expect, at least. Let's go over the other three and see why they are so weird:
- Date.getDay() returned 2 instead of 28
- Because getDay() gives you the week day and not the day of the month. Which is absolutely idiotic. So, you have to use getDate() instead. Which means the name is a lie, becasue the logical thing for getDate() to return is the whole date.
- Date.getMonth() returned 4 instead of 5
- Date.getYear() returned 113 instead of 2013
- Because it uses offset-from-1900. Which is amazing, and I had never heard of a language doing in a standard type. Because why? So, use getFullYear() instead.
So, this produces that I want:
Unless... the day or month are lower than 10, in which case it's missing the left-padding zero. Luckily, for the purpose I was using it, it worked anyway. Because OF COURSE there's no included function to left-pad a string. You have to do it by addition. Or, of course, add a 3rd party function that's out there, in the internet, somewhere.