www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: Date() format as 'mm/dd/yyyy'

  1. #1
    Join Date
    May 2005
    Posts
    9

    Date() format as 'mm/dd/yyyy'

    I am using Date() to display the current date on my page. It displays as "Fri May 27 08:58:21 2005"

    Is there i can use to format this to display as '05/27/2005' instead?

    Thanks,

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    PHP Code:
    <script type="text/javascript">
    var 
    today = new Date();
    var 
    dd today.getDate();
    var 
    mm today.getMonth()+1;//January is 0!
    var yyyy today.getFullYear();
    if(
    dd<10){dd='0'+dd}
    if(
    mm<10){mm='0'+mm}
    onload = function(){
    alert('Today is '+mm+'/'+dd+'/'+yyyy)
    }
    </script> 

  3. #3
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Why convert it from a clear format to one that will be very confusing for people outside of the USA.

    For example:

    06/12/05 means 5th December 2006 to people in Japan and 6th December 2005 to people in Europe and Australia, only in the US does it mean 12th June 2005.

  4. #4
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    If you use
    Code:
    alert (new Date().toDateString()
    it is almost certain to be understood by the user. It uses the date format according to the user's locale settings. I've always wanted to do something like the following:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
     "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="Content-Script-Type" content="text/javascript">
    <meta name="Content-Style-Type" content="text/css">
    <title>Example</title>
    
    <script type="text/javascript">
    <!--
    if (document.getElementsByTagName) onload = function () {
    	var e, i = 0;
    	while (e = document.getElementsByTagName ('SPAN')[i++]) {
    		if (e.className == 'date') e.firstChild.data = new Date (e.firstChild.data).toDateString()
    	}
    }
    // -->
    </script>
    
    </head>
    <body>
    <h4>ETAOIN SHRDLU CMFGYP WBVKXJ QZ</h4>
    <p>Investigationes demonstraverunt <span class="date">27 May 2005</span> lectores legere me lius quod ii legunt saepius.</p>
    </body>
    </html>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  5. #5
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    I've written this code for several people over and over, it be best to make the functional a prototype-function.
    Code:
    <script type="text/javascript">
    
    Date.prototype.defaultView=function(){
    var dd=this.getDate();
    if(dd<10)dd='0'+dd;
    var mm=this.getMonth()+1;
    if(mm<10)mm='0'+mm;
    var yyyy=this.getFullYear();
    return String(mm+"\/"+dd+"\/"+yyyy)
    }
    
    </script>
    <script type="text/javascript">
    
    var today = new Date(); 
    alert('Today is '+today.defaultView()) 
    
    </script>
    That function is quite useful, I'll add it to my prototype function collection thread.

  6. #6
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    I've a one-liner version around here some where...
    Code:
    <script type="text/javascript">
    <!--
    Date.prototype.toMMDDYYYYString = function () {return isNaN (this) ? 'NaN' : [this.getMonth() > 8 ? this.getMonth() + 1 : '0' +  (this.getMonth() + 1), this.getDate() > 9 ? this.getDate() : '0' + this.getDate(),  this.getFullYear()].join('/')}
    
    alert (new Date().toMMDDYYYYString())
    // -->
    </script>
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  7. #7
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    7,974
    Those functions display the date the wrong way around. Most of the world expects the date to be DD/MM/YYYY. You are much better off allowing the date to be formatted based on the locale set on the individual computer that the person is using to display the page so that the date appears in a format that they are expecting.

  8. #8
    Join Date
    Dec 2002
    Location
    Taiwan
    Posts
    1,297
    Quote Originally Posted by felgall
    Those functions display the date the wrong way around. Most of the world expects the date to be DD/MM/YYYY. You are much better off allowing the date to be formatted based on the locale set on the individual computer that the person is using to display the page so that the date appears in a format that they are expecting.
    If you're targeting an audience that expects it in MM/DD/YYYY though, then it doesn't really matter that much does it? If the site's made for American users, then that first method's probably going to work just fine.

  9. #9
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    Quote Originally Posted by felgall
    Most of the world expects the date to be DD/MM/YYYY.
    From what I'm told, most of the world expects YYYY-MM-DD, that being the format common in China.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  10. #10
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by Charles
    I've a one-liner version around here some where...
    Code:
    <script type="text/javascript">
    <!--
    Date.prototype.toMMDDYYYYString = function () {return isNaN (this) ? 'NaN' : [this.getMonth() > 8 ? this.getMonth() + 1 : '0' +  (this.getMonth() + 1), this.getDate() > 9 ? this.getDate() : '0' + this.getDate(),  this.getFullYear()].join('/')}
    
    alert (new Date().toMMDDYYYYString())
    // -->
    </script>
    Shouldn't you remove the quotes around NaN ?

  11. #11
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    Quote Originally Posted by BigMoosie
    Shouldn't you remove the quotes around NaN ?
    In this case, no. We want to return a string, 'NaN', and not the value NaN. Returning the value NaN would seem to work because if, say, you tried to pass it to the alert function said function would treat it as Number(NaN).toString() - which returns the string 'NaN'.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  12. #12
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Yes but what if you wanted to check that the return value was valid? For example:

    if (typeof moose=="string") {}
    else {}

    You say it "seems" to work, but for what reason does it not?

  13. #13
    Join Date
    Feb 2005
    Location
    USA
    Posts
    1,534
    Quote Originally Posted by Charles
    If you use
    Code:
    alert (new Date().toDateString()
    it is almost certain to be understood by the user. It uses the date format according to the user's locale settings.
    Is there a way then to test how the locale setting is set and format the date accordingly?

  14. #14
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,278
    If you want to know if the Date is valid then you wouldn't call Date().toString() you would simply use isNaN (Date).

    Date.toLocaleString() retrurns the date and time according to the user's locale settings.
    Date.toDateString() retrurns the date according to the user's locale settings.
    Date.toTimeString() retrurns the time according to the user's locale settings.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  15. #15
    Join Date
    Jan 2005
    Location
    Los Angeles, CA
    Posts
    4,887
    Quote Originally Posted by BigMoosie
    Shouldn't you remove the quotes around NaN ?
    There is no date that can produce a value of "NaN" besides from invalid entries. So it doesn't matter if you return anything from "NaN" to false

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles