You're here: Snippet Directory » HTML/JavaScript (65)
Language:

JavaScript clone of VB's FormatDateTime()

Language: English
Programming Language: JavaScript
Published by: pjjt [not registered]
Last Update: 5/15/2006
Views: 425


Description

Mirror's the functionality of the built-in FormatDateTime() function in VB.Constant Value Description vbGeneralDate 0 Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed. vbLongDate 1 Display a date using the long date format specified in your computer's regional settings. vbShortDate 2 Display a date using the short date format specified in your computer's regional settings. vbLongTime 3 Display a time using the time format specified in your computer's regional settings. vbShortTime 4 Display a time using the 24-hour format (hh:mm).

Code

1 function FormatDateTime(DateValue, iFormat) { 2 var strDate = ""; 3 var dtmNow = (DateValue) ? new Date(DateValue) : new Date; 4 switch ((!iFormat) ? 0 : iFormat) { 5 case 0 : //vbGeneralDate 6 // Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed. 7 // 1/1/2000 1:00:00 PM 8 strDate = (dtmNow.getMonth() + 1) 9 + '/' + dtmNow.getDate() 10 + '/' + FixYear(dtmNow, DateValue); 11 strDate += " " + ((dtmNow.getHours()<=12) ? ((dtmNow.getHours()==0) ? '12' : dtmNow.getHours()) : dtmNow.getHours() - 12) 12 + ':' + ((dtmNow.getMinutes()<10) ? '0' + dtmNow.getMinutes() : dtmNow.getMinutes()) 13 + ':' + ((dtmNow.getSeconds()<10) ? '0' + dtmNow.getSeconds() : dtmNow.getSeconds()) 14 + ((dtmNow.getHours()<12) ? ' AM' : ' PM'); 15 break; 16 17 case 1 : //vbLongDate 18 //Display a date using the long date format specified in your computer's regional settings. 19 // Wednesday, November 29, 2000 20 strDate = (new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'))[dtmNow.getDay()] + ", "; 21 strDate += (new Array('January', 'February ', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'))[dtmNow.getMonth()] + " "; 22 strDate += dtmNow.getDate() + ", "; 23 strDate += FixYear(dtmNow, DateValue); 24 break; 25 26 case 2 : //vbShortDate 27 //Display a date using the short date format specified in your computer's regional settings. 28 // 11/29/2000 29 strDate = (dtmNow.getMonth() + 1) + '/' + dtmNow.getDate() + '/' + FixYear(dtmNow, DateValue); 30 break; 31 32 case 3 : //vbLongTime 33 //Display a time using the time format specified in your computer's regional settings. 34 // 11:02:07 AM 35 strDate = " " + ((dtmNow.getHours()<=12) ? ((dtmNow.getHours()==0) ? '12' : dtmNow.getHours()) : dtmNow.getHours() - 12) 36 + ':' + ((dtmNow.getMinutes()<10) ? '0' + dtmNow.getMinutes() : dtmNow.getMinutes()) 37 + ':' + ((dtmNow.getSeconds()<10) ? '0' + dtmNow.getSeconds() : dtmNow.getSeconds()) 38 + ((dtmNow.getHours()<12) ? ' AM' : ' PM'); 39 break; 40 41 case 4 : //vbShortTime 42 //Display a time using the 24-hour format (hh:mm). 43 // 11:02 44 strDate = dtmNow.getHours() + ':' + dtmNow.getMinutes(); 45 break; 46 47 default : 48 return FormatDateTime(DateValue, 0); 49 } 50 return strDate; 51 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS