0

When I use var d = new Date() I get a huge string like:

Wed Jan 29 2014 11:36:56 GMT-0800 (Pacific Standard Time)

How do I just return this:

Jan 29,2014

Without using substring, etc to parse it out the string, is there a natural function that will do this?

Control Freak
  • 12,965
  • 30
  • 94
  • 145

4 Answers4

2
var d = new Date();
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var str = months[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
console.log(str);

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
0
d.getMonth();
d.getYear();

etc...

http://www.w3schools.com/jsref/jsref_obj_date.asp

Maybe d.toDateString() is the easiest solution.

http://www.w3schools.com/jsref/jsref_todatestring.asp

Jared Price
  • 5,217
  • 7
  • 44
  • 74
0

I had this problem also. JavasCript Date Library is quite dirty. I suggest you to use DateJS. I'm sure you will get whatever you need about dates and time with this library easily.

Here the link to DateJS

Regards

MarcoGarzini
  • 82
  • 10
  • 1
    _"JavasCript Date Library is quite dirty."_ What does that mean? And why on earth should he use an entire library for something so basic? – j08691 Jan 29 '14 at 19:46
  • 1
    ^ Agreed, using an entire library is much dirtier than creating a function to do it in this particular format only. – Control Freak Jan 29 '14 at 19:49
  • It means in my opinion that you have to write a few lines for get a basic thing like is date and time. I used Javascript Own date library and then I used DateJs. I don't know if you're a big fan of Javascript Library and I hurt you telling this but it's my experience and my opinion. Edit: I hope you are not trying to make a calendar hahah, just kidding. – MarcoGarzini Jan 29 '14 at 19:52
  • @Zee Tee Honestly,I just only see a poor person who is crying for help because have no idea about how to print the day in he lives in a computer screen. Also I see that you are this kind of people that this community didn't need.Pathetic – MarcoGarzini Jan 29 '14 at 20:15
  • Yup, you figured me out lol – Control Freak Jan 29 '14 at 20:32
0

There's also XDate; simple yet powerful.

Juan Lhc
  • 327
  • 3
  • 11