just2comment Posted January 25, 2010 Share Posted January 25, 2010 date.toString() usually returns a string including UTC or GMT information, which I don't neet to use. I'm using a rather "poor" code to avoid including UTC/GMT information in the string date: <html><body> <h1>Hi!</h1> <script type="text/javascript"> var todayStr = (new Date()).toString(); document.write(todayStr + "<br> <br>"); if (todayStr.indexOf("GMT") > 0) document.write(todayStr.substr(0, todayStr.indexOf("GMT")) + "<br> <br>"); else if (todayStr.indexOf("UTC") > 0) document.write(todayStr.substr(0, todayStr.indexOf("UTC")) + "<br> <br>"); </script></body><html> 1) Is there another possible format other than GMT/UTC (in which case my code wouldn't work)?2) How would be the regexp to get the "day month number year hour:min:sec"? Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 Why use a regexp? The date object has methods to return any part of the date. Link to comment Share on other sites More sharing options...
just2comment Posted January 26, 2010 Author Share Posted January 26, 2010 Maybe I've missed something, but month and day are returned in numbers, and I would like to use "names". By the way, I'm trying with regexps (just as a learning exercise right now), and the best I have so far isvar patt1=/[A-Z][a-z]{2} [A-Z][a-z]{2} [0-9]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2}/;document.write(todayStr.match(patt1));I'm trying to find the syntax to repeat the pattern [A-Z][a-z]{2}, but maybe I should use [a-Z]{3} instead... anyway, I'll play a while... my learning curve use to be "slow"... Link to comment Share on other sites More sharing options...
justsomeguy Posted January 26, 2010 Share Posted January 26, 2010 Personally, I would use things like getMonth or getDay and just use a switch statement to print the name instead of the number, but I guess a regular expression would work also. Link to comment Share on other sites More sharing options...
just2comment Posted January 26, 2010 Author Share Posted January 26, 2010 Ok, thanks. Also, toString returns different formats for UTC and GMT, so I think it's better con "construct" the string. Just a way to do this (I include parts of code "adapted" from http://www.w3schools.com/js/tryit.asp?file...s_timing_clock) <html><body> <h1>Hi!</h1> <script type="text/javascript"> var monthArr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); var dayArr = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"); var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); var s = today.getSeconds(); // add a zero in front of numbers < 10 h = inTwoDigits(h); m = inTwoDigits(m); s = inTwoDigits(s); document.write(dayArr[today.getDay()] + " " + monthArr[today.getMonth()] + " " + today.getDate() + " " + today.getFullYear() + " " + h+":"+m+":"+s); function inTwoDigits(i) { if (i < 10) i = "0" + i; return i; } </script></body><html> better/other similar ways are welcome. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 26, 2010 Share Posted January 26, 2010 I wrote this function myself. It uses some of the same format codes as the PHP date() function. It's probably not perfect but it does a good job for me. function FormatDateTime(strFormat, strDate) { //strDate is optional, if it is left out the current date/time is used var arrFormatChars = new Array("y", "Y", "n", "M", "m", "o", "D", "w", "d", "L", "h", "H", "t", "s", "i", "a", "A"); /* y - Two-digit year w - Full day of week name Y - Four-digit year h - Number of hours (twelve-hour) m - Month num w/o zeros H - Number of hours (24-hour) o - Month num w/ zeros t - Number of minutes n - Month abbreviation s - Number of seconds M - Full month name i - Number of milliseconds d - Day (w/o zeros) a - Lowercase am or pm L - Day (w/zeros) A - Uppercase AM or PM D - Day of week (abbr) */ var arrMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); var arrDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); if (strDate != undefined) { //if strDate is not provided set it to current date var formatDate = strDate; } else { var formatDate = new Date(); } if (formatDate != "Invalid Date") { //Extract all the date parts from the date var dYear = formatDate.getFullYear(); var dMonth = formatDate.getMonth(); var dDate = formatDate.getDate(); var dDay = formatDate.getDay(); var dHours = formatDate.getHours(); var dMinutes = formatDate.getMinutes(); var dSeconds = formatDate.getSeconds(); var dMilli = formatDate.getMilliseconds(); var blnValidFormat = false; var strFormattedDate = ""; var arrAssembly = new Array(strFormat.length); //Array used to reassemble the format string //Construct an array that holds the formatted date values to put in place of the format characters var FourDigitYear = dYear.toString(); var TwoDigitYear = FourDigitYear.substr(2); var MonthNumber = (dMonth+1).toString(); if ((dMonth+1) < 10) { var MonthWithZeros = "0"+(dMonth+1).toString(); } else { var MonthWithZeros = (dMonth+1).toString(); } var MonthAbbr = arrMonths[dMonth].toString().substr(0, 3); var MonthName = arrMonths[dMonth].toString(); var DayOfMonth = dDate.toString(); if (dDate < 10) { var DayWithZeros = "0"+dDate.toString(); } else { var DayWithZeros = dDate.toString(); } var DayAbbr = arrDays[dDay].toString().substr(0, 3); var DayName = arrDays[dDay].toString(); if (dHours > 12) { var HoursStd = (dHours-12).toString(); var TimeSuffix = "pm"; } else if (dHours == 0) { var HoursStd = "12"; var TimeSuffix = "am"; } else { var HoursStd = dHours.toString(); var TimeSuffix = "am"; } var HoursMil = dHours.toString(); if (dMinutes < 10) { var Minutes = "0"+dMinutes.toString(); } else { var Minutes = dMinutes.toString(); } if (dSeconds < 10) { var Seconds = "0"+dSeconds.toString(); } else { var Seconds = dSeconds.toString(); } var Milliseconds = dMilli.toString(); var arrFormatStrVals = new Array(TwoDigitYear, FourDigitYear, MonthAbbr, MonthName, MonthNumber, MonthWithZeros, DayAbbr, DayName, DayOfMonth, DayWithZeros, HoursStd, HoursMil, Minutes, Seconds, Milliseconds, TimeSuffix.toLowerCase(), TimeSuffix.toUpperCase()); //Loop through the format string comparing each character to the array of format characters and populate the assembly array var CurrChar = ""; var blnCharFound = false; var xbound = strFormat.length; var ybound = arrFormatChars.length; for (x=0; x<xbound; x++) { CurrChar = strFormat.charAt(x); blnCharFound = false; for (y=0; y<ybound; y++) { if (CurrChar == arrFormatChars[y]) { blnCharFound = true; blnValidFormat = true; arrAssembly[x] = arrFormatStrVals[y]; break; } } if (!blnCharFound) { arrAssembly[x] = CurrChar; } } //Assemble the formatted date string xbound = arrAssembly.length; for (x=0; x<xbound; x++) { strFormattedDate+=arrAssembly[x]; } } else { strFormattedDate = "Error!"; throw "Date is invalid"; } if (!blnValidFormat) { strFormattedDate = "Error!"; throw "Format string is invalid"; } return strFormattedDate;} //End function FormatDateTime Link to comment Share on other sites More sharing options...
just2comment Posted January 26, 2010 Author Share Posted January 26, 2010 Thank you, very much. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.