I have a simple snippet which prints full day, month, day of month, hour and minute.
Here is the code: /
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);
I have a simple snippet which prints full day, month, day of month, hour and minute.
Here is the code: http://jsfiddle/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);
Share
edited Jan 7, 2012 at 21:09
Pointy
414k62 gold badges595 silver badges629 bronze badges
asked Jan 7, 2012 at 21:08
jQuerybeastjQuerybeast
14.5k39 gold badges120 silver badges198 bronze badges
3
- It's always a good idea to put the code here, since jsfiddle tends to be unreliable. I've edited it in. – Pointy Commented Jan 7, 2012 at 21:10
-
You might want to look at the datejs library, which has a
toString()
method that acts as a templating system for dates. – Pointy Commented Jan 7, 2012 at 21:12 - Thanks Pointy for the edit. I am on the go on a mobile device so its a little bit hard. I am aware of datejs library but when it es to just a line of code, its useless. – jQuerybeast Commented Jan 7, 2012 at 21:24
4 Answers
Reset to default 1Just add this:
var txt = now.toLocaleDateString().replace(/\b[a-z]+\b/gi,function($0){return $0.substring(0,3)});
Code: http://jsfiddle/raNms/1/
Here's a great way of doing it: http://www.webdevelopersnotes./tips/html/getting_current_time_using_javascript.php3
Here is what your code looks like with the changes:
Output:
Sat, Jan 7 2012 04:21 pm
Code:
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 weekdayNames = new Array("Sun", "Mon", "Tuey",
"Wed", "Thu", "Fri", "Sat");
var monthNames = new Array("Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec");
//var dateString = now.toLocaleDateString();
var weekday = weekdayNames[now.getDay()];
var month = monthNames[now.getMonth()];
var dateString = weekday + ', ' + month + ' ' + now.getDate() + ' ' + now.getFullYear();
var time = dateString +' '+h+':'+m+' '+ampm
$('body').html(time);
I also created a jsFiddle for it http://jsfiddle/luisperezphd/raNms/2/
A easy way:
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;
t = now.toLocaleDateString();
var time = t.split(',')[0].substring(0,3) + ', ' +
t.split(',')[1].substring(1,4) + ' ' +
t.split(',')[1].split(' ')[2] +
t.split(',')[2] + ', ' +
' '+h+':'+m+' '+ampm
$('body').html(time);
Output:
Sat, Jan 07 2012, 10:20 pm
Use a suitable library, such as Globalize.js. Ad hoc code for dealing with things like this takes more work, tends to be less reliable, and means a burden when you need to modify your software.
Example:
Globalize.format(today,'ddd, MMM d, yyyy') + ' ' + Globalize.format(today,'T')
Produces e.g.
Sat, Jan 7, 2012 11:32:21 PM
You can tune the appearance by modifying the formatting strings.
Notice that toLocaleString
is by definition system-dependent, so it may produce weekday names in any language (or not at all). Here’s what your code produces in my environment on one browser:
“7. tammikuuta 2012 11:26 pm”.