Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateTime Modification 2

Status
Not open for further replies.

cumap

IS-IT--Management
Jul 9, 2007
268
US
Hello,

How can I modify hour on this string of date/time

Fri May 1 13:36:48 PDT 2009

I tried getHours() but getting error "object.expected"

Thanks
 
My goal is to adjust the hour on the string and if there is noway to get the hour to edit, can anyone help me using string functions that somehow pinpoint the hour location so I can come up with a way to replace the hour with more desirable one.
 
<script type="text/javascript">
var day = new Array();
day[0] = "Sun";
day[1] = "Mon";
day[2] = "Tue";
day[3] = "Wed";
day[4] = "Thu";
day[5] = "Fri";
day[6] = "Sat";

var month = new Array();
month[1] = "Jan";
month[2] = "Feb";
month[3] = "Mar";
month[4] = "Apr";
month[5] = "May";
month[6] = "Jun";
month[7] = "Jul";
month[8] = "Aug";
month[9] = "Sep";
month[10] = "Oct";
month[11] = "Nov";
month[12] = "Dec";

var myDate = new Date("Fri May 11 13:36:48 PDT 2009");
var dayOfWeek = day[myDate.getDay()];
var monthName = month[myDate.getMonth()];
var year = parseInt(myDate.getYear()) + 1900;

var dateDate = myDate.getDate();
var hour = myDate.getHours();
var min = myDate.getMinutes();
var sec = myDate.getSeconds();

var timeOfDay = "AM";
if (parseInt(hour) > 12) {
hour = parseInt(hour) - 12;
timeOfDay = "PM";
}

newDate = dayOfWeek + " " + monthName + " " + dateDate + " " + year + " " + hour + ":" + min + ":" + sec + " " + timeOfDay;
alert(newDate);


</script>

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
^ in the "year" the + 1900 is for FF, as it getYear returns a number equal the the number of years since 1900.

For IE you don't need it, so you'll need to build in a browser check...unless someone else has a better way to grab the year.

TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
BTW a GOOGLE search on "javscript format date" found this - which is probably a MUCH better way of doing this...




TIP: trying googling the answer before posting, you'll find that more times than not someone else somewhere has had the same request and posted an answer online.
----
I have recently been semi-converted to ensuring all my code (well most of it) works in both javascript and non-javascript enabled browsers
 
The way to grab the full year is with the Date method getFullYear(). That will give you a 4 digit year in all browsers.

Lee
 
Thanks Vicvirk, your code is perfect for my reformat requirement. My bad trying to have this query getHours.strDateTime() to work. Go figure!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top