0

Would like to find all instances of a MySQL style date or date/time (YYYY-MM-DD or YYYY-MM-DD HH:MM:SS) on an html page using javascript (or jquery if practical) and replace each with a nicely formatted string (like: October 5th, 2010). Looking for the best approach.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
phirschybar
  • 8,357
  • 12
  • 50
  • 66
  • What language are you using to query the data from the mySQL table? – sadmicrowave May 31 '11 at 12:41
  • using php but it's irrelevant as I want the date "masking" to occur client-side. – phirschybar May 31 '11 at 12:43
  • 3
    That should be done server-side. – ThiefMaster May 31 '11 at 12:46
  • 1
    I usually don't ask these types of questions when the questioner explicitly asks for something, but why? when you are already making a call to the server. Cut down on the client side script and put this easy segment in your php: date('D M jS, Y', strtotime( [field] ) ) – sadmicrowave May 31 '11 at 12:48
  • Plus - this is more complicated in javascript as you need to use the new Date() function which does not have a Text Month identifier meaning you need to include a custom created function that assigns a month like 'October' if the month of Date() equals 10. Php's Date() has this built in. – sadmicrowave May 31 '11 at 12:52
  • I usually prefer MySQL's DATE_FORMAT() myself but the reason why I want to do this client-side is that the dates on the pages of this app will come from multiple sources and in some cases I won't have the ability to alter the query or do any pre-formatting before it hits the screen. You'll just have to take my word for it in how this system was designed. :) I truly need to run this client-side! – phirschybar May 31 '11 at 13:34

2 Answers2

1

Do it in php is the correct answer but you don't want to do that. So...

Try this toolkit:

http://www.javascripttoolbox.com/lib/date/examples.php

Put in a date in the 'parsing' box and use yyyy-MM-dd for the format, that should give you what you are after, there on it is probably best you read the instructions.

Oh, and you'll be needing jQuery .each to iterate over whatever your date class is going to be. Or see this post if you don't have classes setup:

jQuery find and replace string

Community
  • 1
  • 1
ʍǝɥʇɐɯ
  • 4,012
  • 7
  • 32
  • 53
  • this led me down the right path. and especially the accepted answer here: http://stackoverflow.com/questions/5115152/jquery-find-and-replace-string which has a link to: http://www.benalman.com/projects/jquery-replacetext-plugin/ – phirschybar Jun 02 '11 at 10:41
0

@phirschybar this person needed the same thing done and accepted an answer linking him to another site where he found the answer by using a custom created date function. Just get the source - add it in your <head> tags and call it with the specific date variable you want to use:

How can I convert string to datetime with format specification in JavaScript?

Hopefully this helps!

Community
  • 1
  • 1
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180