Forum Moderators: open

Message Too Old, No Replies

Changing the format a date is displayed

Changing the format a date is displayed

         

HetzelCreative

6:38 pm on Apr 27, 2011 (gmt 0)

10+ Year Member



I am using a script that I downloaded from this source: [javascriptkit.com ]

I would like to modify it a little bit so that the date is displayed in the format "month/day/year" instead of day/month/year. I would also like it to spell out the month instead of abbreviating it with numbers.

Here is the block of code that I think needs rearranged to change the way the date is displayed:

// datetime parsing and formatting routimes. modify them if you wish other datetime format
function str2dt (str_datetime) {
var re_date = /^(\d+)\-(\d+)\-(\d+)\s+$/;
if (!re_date.exec(str_datetime))
return alert("Invalid Datetime format: "+ str_datetime);
return (new Date (RegExp.$3, RegExp.$2-1, RegExp.$1, RegExp.$4, RegExp.$5, RegExp.$6));
}
function dt2dtstr (dt_datetime) {
return (new String (
dt_datetime.getDate()+"-"+(dt_datetime.getMonth()+1)+"-"+dt_datetime.getFullYear()+" "));
}
function dt2tmstr (dt_datetime) {
return (new String (
));
}



Here is another block that defines the months:

function show_calendar(str_target, str_datetime) {
var arr_months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
var n_weekstart = 0; // day week starts from (normally 0 or 1)

var dt_datetime = (str_datetime == null || str_datetime =="" ? new Date() : str2dt(str_datetime));
var dt_prev_month = new Date(dt_datetime);
dt_prev_month.setMonth(dt_datetime.getMonth()-1);
var dt_next_month = new Date(dt_datetime);
dt_next_month.setMonth(dt_datetime.getMonth()+1);
var dt_firstday = new Date(dt_datetime);
dt_firstday.setDate(1);
dt_firstday.setDate(1-(7+dt_firstday.getDay()-n_weekstart)%7);
var dt_lastday = new Date(dt_next_month);
dt_lastday.setDate(0);


Any help would be greatly appreciated. Thanks!

daveVk

2:55 am on Apr 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



start with this line, rearrange the 3 parts to your liking

dt_datetime.getDate()+"-"+(dt_datetime.getMonth()+1)+"-"+dt_datetime.getFullYear()+" "));

for month text use as month part

arr_months[dt_datetime.getMonth()]