Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How can i publish the date being in custom format

    This is my code

    Code:
    myDate.setMonth(myDate.getMonth()+1); 
    	document.getElementById("demo").innerHTML = myDate.toDateString("m/dd/yy");
    but the output is

    Sat Jul 21 1990

    I don't want the day of week (Sat) or the day number (21)

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    You haven't provided the code that defines how the toDateString method you have added to dates is defined. From the look of that output it appears that it totally ignores the parameter you entered but without seeing the code there is no way to tell for certain.

    If you are going to add a method for formatting the date the way you want rather than just joining the fields together then you ought to use one that works - see http://www.felgall.com/datemethods.htm for one alternative where myDate.format('n/d/y') will return the date in the format you want.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...he does *NOT* want the DAY to show. And he seems to want a 4-digit year and the month name abbreviated.

    So should your method be invoked with myDate.format('M Y') ???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Mar 2007
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts
    when i do this

    Code:
    document.getElementById("demo").innerHTML = myDate.format('jS F, Y');
    nothing displays

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Did you download Felgall's code library???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by felgall View Post
    You haven't provided the code that defines how the toDateString method you have added to dates is defined.

    lol, i'll do it for him since HE didn't add it (brendan did):

    Code:
    new Date().toDateString == function toDateString() { [native code] }

    you don't need a whole library to replace a few simple lines of code, that's like adding jQuery to alter document.title using $("title").html("hello")...

    Code:
    var d=new Date(),
     s= [
       d.getMonth()+1,
       d.getDate(),
       d.getFullYear().toString().slice(2)
      ].join("/");
    
    alert(s); // === "11/16/12"
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Except, again, he *SEEMED* to say he wants the NAME of the month and *NOT* the day of the month, at all.

    So:
    Code:
        var mnames = ["Jan","Feb","Mar","Apr","May","Jun",
                      "Jul","Aug","Sep","Oct","Nov","Dec" ];
    
        var d = new Date();
        alert( mnames[d.getMonth()] + " " + d.getFullYear() );
    But who knows?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    Quote Originally Posted by Old Pedant View Post
    Did you download Felgall's code library???
    obviously not or the date would have displayed - I use that particular combination quite a lot myself, it was one of the main reasons i started creating that library.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    (Actually, I think I knew the answer before I asked the question. I was being nice.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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