Rant on web browsers
Asen Bozhilov
asen.bozhilov at gmail.com
Tue Jun 14 18:33:49 EDT 2011
Chris Angelico wrote:
> I've just spent a day coding in Javascript, and wishing browsers
> supported Python instead (or as well). All I needed to do was take two
> dates (as strings), figure out the difference in days, add that many
> days to both dates, and put the results back into DOM Input objects
> (form entry fields). Pretty simple, right? Javascript has a Date
> class, it should be fine. But no. First, the date object can't be
> outputted as a formatted string. The only way to output a date is "Feb
> 21 2011". So I have to get the three components (oh and the month is
> 0-11, not 1-12) and emit those. And Javascript doesn't have a simple
> format function that would force the numbers to come out with leading
> zeroes, so I don't bother with that.
Actually there is not Date class. There are not any classes in
ECMAScript.
> What if I want to accept any delimiter in the date - slash, hyphen, or
> dot? Can I just do a simple translate, turn all slashes and dots into
> hyphens? Nope. Have to go regular expression if you want to change
> more than the first instance of something. There's no nice string
> parse function (like sscanf with "%d-%d-%d"), so I hope every browser
> out there has a fast regex engine. When all you have is a half-ton
> sledgehammer, everything looks like a really REALLY flat nail...
function formatDate(date) {
return ('000' + date.getFullYear()).slice(-4) + '-' +
('0' + (date.getMonth() + 1)).slice(-2) + '-' +
('0' + date.getDate()).slice(-2);
}
formatDate(new Date());
> Plus, Javascript debugging is annoyingly difficult if you don't have
> tools handy. I need third-party tools to do anything other than code
> blind? Thanks.
It depends on the environment. It is good idea to read c.l.js and
JSMentors.
More information about the Python-list
mailing list