SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Date Format

    Hi, I have some code that I'm using for a site that sells massage chairs. Basically, it says "Estimated Delivery:" and then Javascript outputs a date that is exactly 2 weeks from the current date. Here's what I have right now:

    Code:
    <small>Estimated Delivery:</small><br/>
    
    <script>
    DaysToAdd=14;
    var now=new Date();
    var newdate=new Date();
    var newtimems=newdate.getTime()+(DaysToAdd*24*60*60*1000);
    newdate.setTime(newtimems);
    document.write('<font size="5" color="#72D1F6">' + newdate.format('M jS, Y') + '</font>');
    </script>
    At this point I'm not getting any output. When I replace newdate.format(...) with newdate.toLocaleString(), I do get the date, but not formatted.

    Any idea what I'm doing wrong?
    Thanks!

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,607
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    JavaScript doesn't have any fancy formatting option for dates. You need to getDate() getMonth() and getFullYear() etc and build the result yourself. If you want month names or day of the week names then http://javascript.about.com/library/bldatenames.htm provides a simple way to retrieve them the same way you retrieve the other date values.

    If you want a date format function to retrieve dates in a specified format then you'd need to make it yourself.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    You can use this javascript date format method, which extends the date object in useful ways.

    ... + newdate.format('mmm dd, yyyy') + ...
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,607
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    If you really want to use the PHP style formatting commands then I have just written a JavaScript date format method that supports all of the characters where the format makes sense in JavaScript (26 of them in all). See http://javascript.about.com/library/bldateformat.htm for a working example, a copy of the code, and a list of all the commands that the format supports.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, thanks guys! I'll give those a whirl.

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,607
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ethayne View Post
    Alright, thanks guys! I'll give those a whirl.
    I'd like to thank you for giving me the idea to write that script.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well then you're most welcome!


Tags for this Thread

Bookmarks

Posting Permissions

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