Date HTML Help

Discussion in 'Web Design and Development' started by MatthewLTL, Mar 24, 2015.

  1. MatthewLTL, Mar 24, 2015
    Last edited by a moderator: Mar 24, 2015

    MatthewLTL macrumors 68000

    MatthewLTL

    Joined:
    Jan 22, 2015
    Location:
    Rochester, MN
    #1
    I am trying to get a proper code for my website. there is this code:
    Code:
    <script>
    var d = new Date();
    document.getElementById("demo").innerHTML = d.toDateString();
    </script>
    which will display the date like this: Tue Mar 25 2015.

    I want the date to output EXACTLY like this:
    Code:
    Tuesday, March 25, 2015
    all i need to know is that code (or modification to the above code) to make it display the way i want.
     
  2. D.T., Mar 24, 2015
    Last edited: Mar 24, 2015

    D.T. macrumors 603

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #2
    You're going to need to either:

    1) Access the various date object properties and build the date format you'd like, i.e., date.getDay() + " " + date.getDate() [etc]

    2) Use an additional library. I use Moment.js for all my JS date/time formatting (both server and client side).

    JS doesn't have an inherent formatter like some other langs (ex: new Date("mm-dd-yyyy") )
     
  3. redheeler macrumors 603

    redheeler

    Joined:
    Oct 17, 2014
    #3
    Yup, in PHP it's as simple as $date = date('l, F j, Y').
     
  4. D.T. macrumors 603

    D.T.

    Joined:
    Sep 15, 2011
    Location:
    Vilano Beach, FL
    #4
    It's pretty common in most langs/frameworks, PHP like your example, Ruby/Rails has the handy date.to_formatted_s that takes a formatting symbol, C#/.NET with the handy date.ToString() with standard-ish formatting strings.

    It's kind of a silly omission on the part of Javascript, and adding a library is pretty trivial (though it does create kind of a silly dependency).

    I've recently been converting (to get my feet wet) some of a Web API currently in Nodejs to Go, and the latter has brilliant date formatting, you use a mnemonic to express a date (and/or time), so no cryptic codes (that after 20-something years I still forget ...)

    :cool:
     

Share This Page