Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Replace Date's full day name and full month name with 3 letters. Tuesday > Tue

Tags:

javascript

I have a simple snippet which prints full day, month, day of month, hour and minute.

Here is the code: http://jsfiddle.net/raNms/

I want to change where

Monday > Mon
Tuesday > Tue
...

and months:

January > Jan
February > Feb
...

Can it be done before appending to body? I don't want to replace the appended text but to print it correctly from the beginning.

The JavaScript:

var now= new Date(),
    ampm= 'am',
    h= now.getHours(),
    m= now.getMinutes(),
    s= now.getSeconds();
    if(h>= 12){
        if(h>12)h-= 12;
        ampm= 'pm';
    }
    if(h<10) h= '0'+h;
    if(m<10) m= '0'+m;
    var time = now.toLocaleDateString()+' '+h+':'+m+' '+ampm

   $('body').html(time);
like image 271
jQuerybeast Avatar asked Nov 04 '22 08:11

jQuerybeast


1 Answers

Just add this:

var txt = now.toLocaleDateString().replace(/\b[a-z]+\b/gi,function($0){return $0.substring(0,3)});

Code: http://jsfiddle.net/raNms/1/

like image 156
Ivan Castellanos Avatar answered Nov 09 '22 13:11

Ivan Castellanos