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

Thread: Change date format in javascript

  1. #1
    Join Date
    Nov 2004
    Posts
    9

    Change date format in javascript

    hi guys, this is one date that i have
    Thu Jan 10 00:00:00 UTC+0800 2002
    can i change is to any other format eg. "dd-MM-yyyy hh:mm:ss" or something else using javascript on the client side?
    any help appreciated...
    thx

  2. #2
    Join Date
    Dec 2002
    Location
    Taiwan
    Posts
    1,297
    PHP Code:
    <script type="text/javascript">
    <!--
    var 
    now = new Date();
    var 
    dateString now.getMonth() + "-" now.getDate() + "-" now.getFullYear() + " "
                   
    now.getHours() + ":" now.getMinutes() + ":" now.getSeconds();
    alert(dateString);
    //-->
    </script> 
    I think that should work for you...

  3. #3
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    <script type="text/javascript">
    <!--
    // You can have the date and time formatted according to your user's set preference:
    alert (new Date().toLocaleString());

    //or just the date:
    alert (new Date().toDateString());

    //or just the time:
    alert (new Date().toTimeString());

    //Those will keep your users happy, but if you want to be a control freak about it then you can overwrite the "toString()" method for the entire Date class:

    Date.prototype.toString = function () {return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'] [this.getDay()], this.getDate(), ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], this.getFullYear()].join(' ')}

    alert (new Date());
    // -->
    </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

  4. #4
    Join Date
    Nov 2004
    Posts
    9
    no no no......
    i already have this string
    Thu Jan 10 00:00:00 UTC+0800 2002
    which i DONT WANT....


    i want to format the above sting into a normal date string
    something like
    "dd-MMM-yyyy hh:mm:ss"


    so can i format the string that i have - > Thu Jan 10 00:00:00 UTC+0800 2002
    to a NORMAL date...
    like
    "dd-MM-yyy" or something???

    thx...

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    <script type="text/javascript">
    <!--
    // Yes, yes, yes... All you have to do is run that string past the Date constructor and use any of the above:

    Date.prototype.toString = function () {return isNaN (this) ? 'NaN' : [this.getDate() > 9 ? this.getDate() : '0' + this.getDate(), this.getMonth() > 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getFullYear()].join('-')}

    alert (new Date('Thu Jan 10 00:00:00 UTC+0800 2002'));
    // -->
    </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

  6. #6
    Join Date
    Nov 2004
    Posts
    9
    wat is this line supposed to do?
    alert (new Date('Thu Jan 10 00:00:00 UTC+0800 2002'));
    is is converting to a normal format?
    i DONT WANT this format :'Thu Jan 10 00:00:00 UTC+0800 2002'

    will using the new date constructor change it?

  7. #7
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    Cut and past my example above, run it and tell me what you get.
    “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

  8. #8
    Join Date
    Nov 2004
    Posts
    9
    hi charles,
    also, i would really appreciate if you could tell me what this is being used by you for,,,
    Date.prototype.toString = function () {return isNaN (this) ? 'NaN' : [this.getDate() > 9 ? this.getDate() : '0' + this.getDate(), this.getMonth() > 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getFullYear()].join('-')}

    i dont understand wat the above line of code is doin....

    thx very much....

  9. #9
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    When you try to use a Date object as a string, and the alert() method only takes strings, JavaScript calls the object's toString() method. That little piece of script simply overwrites the Date object's native toString() method and formats all dates according to your request.
    “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
    Nov 2004
    Posts
    9
    Originally posted by Charles
    Cut and past my example above, run it and tell me what you get.
    i pasted this
    Date.prototype.toString = function () {return isNaN (this) ? 'NaN' : [this.getDate() > 9 ? this.getDate() : '0' + this.getDate(), this.getMonth() > 8 ? this.getMonth() + 1 : '0' + (this.getMonth() + 1), this.getFullYear()].join('-')}

    alert (new Date('Thu Jan 10 00:00:00 UTC+0800 2002'));

    i get error. cant eval function...


    btw : i pasted as u gave me...
    i pasted Date.prototype.toString as it is,,, i am not supposed to change anything to it am i??
    the prototype keyword u are using, is really a keyword right?
    thx...

  11. #11
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,279
    It must be a cut and paste error for it works just fine on this end. And yes, "prototype" is correct.

    Perhaps if you post a link.
    “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
    Nov 2004
    Posts
    9
    yea it works....
    would u pls explain wat are u doin in the code???
    i dont understand wats hapenin in ur function.....

    Date.prototype.toString = function () {return [['Sunday,', 'Monday,', 'Tuesday,', 'Wednesday,', 'Thursday,', 'Friday,', 'Saturday,'] [this.getDay()], this.getDate(), ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] [this.getMonth()], this.getFullYear()].join(' ')}


    also wat is prototype??

    thx a lot...

  13. #13
    Join Date
    Nov 2004
    Posts
    9
    the reason why i am asking is, cause i'd like to modify ur function for other purposes when i need different kind of date....
    thx very much....

  14. #14
    Join Date
    Mar 2003
    Posts
    575
    rthomas, why don't you write your own code?

    Do you know how to write a function in JavaScript?

    This date stuff is "baby stuff" as far as JavaScript is concerned. However, JavaScript can be a bit evil at times, so maybe on your box it isn't baby stuff or isn't working right...

    Here is a function for formatting numbers, to get you started.

    PHP Code:
    function zeropad(number,minLength) {
    outstr=number.toString();
    while(
    outstr.length<minLengthoutstr='0'+outstr;
    return 
    outstr;

    Please read this code and if it isn't immediately obvious to you what this code is supposed to do, you should learn more JavaScript before attempting to write these programs on your own.

  15. #15
    Join Date
    Jul 2004
    Posts
    986
    Could this be a homework assignment?
    If so, I can understand how hard it must be to turn in someone elses work without knowing what it does or how it works...

    .....Willy

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