0

My question has 2 parts. First I would like to remove the last 9 characters from a generated string using jQuery eg

2011-04-04T15:05:54

which would then leave the date remaining. I know to use the .substring function. After the time is removed I need to reformat the date into this format

08.04.11 (for eg)

I am learning how to write jQuery and would appreciate any help with this code.

naugtur
  • 16,827
  • 5
  • 70
  • 113
madameFerry
  • 219
  • 1
  • 6
  • 20

1 Answers1

1

This is what you do with pure javascript, jQuery is not needed.

var d1=new Date();
d1.toString('yyyy-MM-dd');       //returns "2009-06-29"
d1.toString('dddd, MMMM ,yyyy')  //returns "Monday, June 29,2009"

Another example to fit your case:

var d1=new Date();
d1.toString('dd.MM.yy');       //returns "08.04.11"

More:

Where can I find documentation on formatting a date in JavaScript?

Community
  • 1
  • 1
naugtur
  • 16,827
  • 5
  • 70
  • 113
  • +1 for promoting a javascript solution. jQuery is not required to solve the problem. – Andy Rose Apr 12 '11 at 08:29
  • Thank you for your prompt reply. If I can just clarify how I would use your code to achieve my goal please. – madameFerry Apr 12 '11 at 09:13
  • If my code solves the first problem //Remove time data var comDate = sDate.substring(0,sDate.length - 9); $('p.date').html(comDate); – madameFerry Apr 12 '11 at 09:13
  • Ok to finish this post off and to clarify my question. This is the code that I ended up writing which achieved my goals. //Remove time data and reformat date var comDate = sDate.substring(0,sDate.length - 9); var array = comDate.split('-'); var year = array[0]; var month = array[1]; var day = array[2]; $('div.date').html(''+month+'.'+day+'.'+year+''); – madameFerry Apr 12 '11 at 11:10
  • And why do you do that? Date object returns days months and years separately with no string operations. And you can put dots in the string in `toString` – naugtur Apr 12 '11 at 18:23