Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Try this: date.toLocaleDateString("en-US", {month: "numeric", day: "numeric"});


I want a specific format to our proprietary systems. In C# I can just say "date.Format("dd.MM.YYYY")" or what ever I want.


Granted that's a bit tidier than doing this in JS:

var myProprietaryDateFunction = function(dt) { return dt.getDate()+'.'+dt.getMonth()+'.'+dt.getFullYear(); };

But in JS, you can pass that function around like a village bicycle -when I think back to my C# days, it makes me wonder how I ever did without functions as first class objects and a slew of other really great things about JS. A lot of those things are brainbangers at first, to be sure - but when you finally grasp them (for me, at least) you start to see that the things that make JS "ugly" to the novice are the same things that make it powerful and elegant in the hands of a master. To each his own - and there are things I do really miss about C# from time to time... but for me, I am all too happy trade the tidier date.Format() for the more powerful underlying functional constructs any day.


when I think back to my C# days, it makes me wonder how I ever did without functions as first class objects and a slew of other really great things about JS.

C# has delegates, events, lambda function, properties and async function as language constructs. This is as "first class" as it gets. It also has LINQ.


You forgot to add 1 to dt.getMonth(), because months index 0-11 :)

(and be careful not to accidentally concatenate that 1 instead of add)


uh, this won't have leading zeroes like DD.MM.YYYY format would have




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: