开发者

Jquery first word CAPs

开发者 https://www.devze.com 2023-03-28 01:17 出处:网络
I am using wordpress the_date() function to get the post date , and it returns me <h2 class=\"the_date>Friday, August 12, 2011</h2>

I am using wordpress the_date() function to get the post date , and it returns me

<h2 class="the_date>Friday, August 12, 2011</h2>

What I need is to have the friday to be caps without e开发者_如何学运维ffecting other words aka FRIDAY, August 12, 2011.

Can this be done with jQuery or javascript ?


Yes, using JavaScript's String.toUpperCase():

var date = "Friday, August 12, 2011";
var pieces = date.split(" ");
pieces[0] = pieces[0].toUpperCase();
alert(pieces.join(" "));

Demo.


Keep in mind, this is pseudocode, not checked for syntax/runnability.

var myString = the_date()
myString = myString.SubString(0, myString.indexOf(',')).ToUpperCase() + myString.Substring(myString.indexOf(','));

The syntax may be off a bit, and you may need to add/subtract 1 on either of the indexOf calls to include the proper character, but this method should work.


In your WordPress theme, you could use

<?php strtoupper(the_time('l')) . ", " . the_time('F j, Y'); ?>

instead of the_date() to get your desired output.

Update

jQuery solution if you wish to go that route:

HTML

<h2 class="the_date">Friday, August 12, 2011</h2>

jQuery

var the_date = $('.the_date').html().split(', ');
the_date = [the_date[0].toUpperCase(), the_date.slice(1).join(', ')].join(', ');
$('.the_date').html(the_date);

jsFiddle demo: http://jsfiddle.net/tcA4y/

Updated jsFiddle demo: http://jsfiddle.net/tcA4y/1/


javascript can do this. you can substring the first word out, capatalize it, and put it back.


You'd have to use either substr/substring or split/join. I'd be inclined to use split/join:

// breaks into array based on comma
var dt = 'Friday, August 12, 2011'.split(','); 
// reassign 0 index to upper case (so Friday, in this case)
dt[0] = dt[0].toUpperCase();
console.log( dt.join( ',' ) ); // FRIDAY, August 12, 2011

And here's the substr version:

var dt = 'Friday, August 12, 2011';
// I'm using 'y' here because days which do not end in Y are very rare.
var yindex = dt.indexOf("y");
     // everything before and including y to upper case
dt = dt.substr(0, yindex + 1).toUpperCase() + 
     // then add the balance to the end.
     dt.substr( yindex + 1);
console.log( dt );
0

精彩评论

暂无评论...
验证码 换一张
取 消