WebMW
WebMW

Reputation: 524

Removing the day from a date string with jQuery

I am trying to remove the "day" from every possible date occurrence on a page.

This is to make jQuery turn every date in the format of "08/22/2012" into "08/2012"

I was able to do this with this code: Replacing wildcard text using jquery

See my fiddle for more information: http://jsfiddle.net/CfZjF/223/

But it just isn't working within this table layout, regardless of what I have tried.

Another problem will be to specify the day specifically (maybe with wildcards?)-- that is the 2 numbers between the forward-slashes: /xx/, but please see the fiddle for more info.

Any ideas on how I can pull this off?

Upvotes: 2

Views: 1581

Answers (3)

Phillip Schmidt
Phillip Schmidt

Reputation: 8818

Since all your dates are in the form xx/xx/xxxx, using a simple split() would always split it into an array with these values:

xx,xx,xxxx

So, something like this:

var totalDate = $("whateverYourDateSelectorMightBe");
var daysInMiddle = totalDate.val().ToString().split(",")[1];

so then you could do:

totalDate.val(totalDate.val().ToString().replace(daysInMiddle + "/",""));

Note that there are much cleaner ways to do this. I just did it this way because I think it better explains what I was trying to do.

Upvotes: 0

aknosis
aknosis

Reputation: 4308

I think you should individually traverse the table cells instead of trying to globally muck with the entire rows HTML.

This assumes that your data is formatted as in your jsFiddle.

Updated fiddle: http://jsfiddle.net/GKrCS/

$('tr').each(function(){
       $('td',this).not(':first').text(
           function(){
               return $(this).text().replace(/\/[0-9]+\//,'/');
           });
});

Upvotes: 1

Bergi
Bergi

Reputation: 664589

Try

str.replace(/\/\d+\//g, "/");

Or be more specific by replacing /(\d{2})\/\d{2}\/(\d{4})/g with "$1/$2" or something…

(Updated fiddle)

Upvotes: 2

Related Questions