SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
Dec 20, 2002, 19:19 #1
- Join Date
- Dec 2002
- Location
- Denver, CO
- Posts
- 2,877
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
simple date and time script needed
i need a very simple javascript code that i can insert into a table that will display the current date in this format:
"Friday, December 20, 2002"
and the time in this format:
"7:20 PM"
any suggestions?
-
Dec 20, 2002, 19:51 #2
- Join Date
- Nov 2002
- Posts
- 51
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Here's some code...
Code:MyDate = { // Private Methods and Properties days : ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"], months : ["January","February","March","April","May","June","July","August","September","October","November","December"], getHours : function(theHour){ return (theHour>12 || theHour==0) ? Math.abs(theHour-12) : theHour; }, addZero : function(theNumber){ return (theNumber>9) ? theNumber : "0"+theNumber; }, getAmPm : function(theHour){ return (theHour<12) ? "AM" : "PM"; }, // Public Methods setDate : function(){ var now = new Date(); this.hour = this.getHours(now.getHours()); this.minute = this.addZero(now.getMinutes()); this.second = this.addZero(now.getSeconds()); this.ampm = this.getAmPm(now.getHours()); this.day = this.days[now.getDay()]; this.date = now.getDate(); this.month = this.months[now.getMonth()]; this.year = now.getFullYear(); }, getDate : function(){ return this.day+", "+this.month+" "+this.date+", "+this.year; }, getTime : function(){ return this.hour+":"+this.minute+" "+this.ampm; } }
http://travis.squidfingers.com/date.htmltravis
-
Dec 23, 2002, 10:19 #3
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Another way
Save the below code as 'date.class.js' or whatever you like.Code:function MyDate( dateStr ) { if ( typeof dateStr != 'undefined' ) { var d = new Date( dateStr ); this.dateString = dateStr; } else if (typeof dateStr == 'object') var d = dateStr; else var d = new Date(); var months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]; var weekDays = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ]; var monthDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ]; this.epoch = d.getTime(); this.epochS = Math.round( this.epoch / 1000 ); this.year4 = d.getFullYear(); this.year2 = parseInt( this.year4.toString().substring( 2 ) ); this.leap = ( this.year4 % 400 == 0 ) ? true : ( this.year4 % 4 == 0 && this.year4 % 100 != 0 ) ? true : false; if ( this.leap ) monthDays[1]++; this.mnth = d.getMonth(); this.month = this.mnth + 1; this.month_2 = leadingZero(this.month); this.monthName = months[this.mnth]; this.monthNameShort = this.monthName.substring( 0,3 ); this.days = monthDays[this.mnth]; this.dy = d.getDay(); this.day = this.dy + 1; this.dayName = weekDays[this.dy]; this.dayNameShort = this.dayName.substring( 0,3 ); this.date = d.getDate(); this.date_2 = leadingZero( this.date ); this.suffix = ( this.date % 10 == 1 ) ? "st" : (this.date % 10 == 2) ? "nd" : (this.date % 10 == 3) ? "rd" : "th"; this.hours24 = d.getHours(); this.hours24_2 = leadingZero( this.hours24 ) this.hours12 = ( this.hours24 == 0 ) ? 12 : ( this.hours24 > 12 ) ? this.hours24-12 : this.hours24; this.hours12_2 = leadingZero( this.hours12 ); this.minutes = d.getMinutes(); this.minutes_2 = leadingZero( this.minutes ); this.seconds = d.getSeconds(); this.seconds_2 = leadingZero( this.seconds ); this.millis = d.getMilliseconds(); this.ampm = ( this.hours24 < 12 ) ? "am" : "pm"; this.GMTstring = d.toGMTString(); this.offset = d.getTimezoneOffset(); function leadingZero( num ) { return ( num < 10 ) ? "0" + num : num.toString(); } } MyDate.prototype.getDayOfYear = function() { var total = 0; var monthDays = [ 31,28,31,30,31,30,31,31,30,31,30,31 ]; if ( this.leap ) monthDays[1]++; for ( var i=0; i<this.mnth; total += monthDays[i++] ) {} total += this.date; return total; } MyDate.prototype.getDate = function( str ) { var p = ['a','A','B','d','D','F','g','G','h','H','i','I','j','l','L','m','M','n','O','r','s','S','t','T','U','w','W','Y','y','z','Z']; var r = new Array(); var i = 0; var delChar = "|"; var sNull = "null".delimit(delChar); r[i++] = this.ampm.delimit( delChar ); r[i++] = this.ampm.toUpperCase().delimit( delChar ); r[i++] = sNull; r[i++] = this.date_2; r[i++] = this.dayNameShort.delimit( delChar ); r[i++] = this.monthName.delimit( delChar ); r[i++] = this.hours12; r[i++] = this.hours24; r[i++] = this.hours12_2; r[i++] = this.hours24_2; r[i++] = this.minutes_2; r[i++] = sNull; r[i++] = this.date; r[i++] = this.dayName.delimit( delChar ); r[i++] = Boolean( this.leap ); r[i++] = this.month_2; r[i++] = this.monthNameShort.delimit( delChar ); r[i++] = this.month; r[i++] = this.offset; r[i++] = this.GMTstring.delimit( delChar ); r[i++] = this.seconds_2; r[i++] = this.suffix.delimit( delChar ); r[i++] = this.days; r[i++] = sNull; r[i++] = this.epochS; r[i++] = this.dy; r[i++] = sNull; r[i++] = this.year4; r[i++] = this.year2; r[i++] = this.getDayOfYear(); r[i++] = sNull; for ( i=0; i<p.length; i++ ) p[i] = "/" + p[i] + "(?!\\|)/g"; return str.arrayReplace( p, r ).replace( /\|/g,"" ); } String.prototype.arrayReplace = function( arrP, arrR ) { var pattern, p, s = this; for ( var i = 0; ( pattern = arrP[i] ); i++ ) { var flags = pattern.substring( pattern.lastIndexOf( "/" ) + 1 ); var regex = pattern.substring( 1, pattern.lastIndexOf( "/" ) ); p = new RegExp( regex, flags ); s = s.replace( p, arrR[i] ); } return s; } String.prototype.delimit = function( del ) { var char, s = ""; for ( var i=0; (char = this.charAt( i ) ); i++ ) s += char + del; return s; }
Code:var d = new MyDate(); document.write(d.getDate("l, F jS, Y")); document.write(d.getDate("h:m A"));
If no argument is passed to the constructor function, MyDate(), then the current date/time will be used. You can also choose to pass it a date string or an existing date object.
Let me know if you'd like more help using this.
Bookmarks