SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    May 2012
    Location
    N.Ireland
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Formatting of ISO date time

    I'm looking to change the format of my date and time stamps on a scroll plugin.

    The code is currently;
    Code:
    { return ("Date: " + d.getDate() + "/" + d.getMonth()+1) + "/" + d.getYear().toString().substr(-2) + " - Time " + d.getHours() + ":" + d.getMinutes() }
    which shows
    [Date: 11/11/12 - Time: 10:0 - 12:00]
    I want it to show:
    [Date: 11/11/12 - Time: 10:00 - 12:00]
    Why is the second zero missing???

    Not sure what I'm doing wrong - the plugin script that deals with it is;

    Code:
          date_formatter: function(d, allday_p) {
            if (allday_p) {
              return "" + (d.getFullYear()) + "-" + (pad_zero(d.getMonth() + 1)) + "-" + (pad_zero(d.getDate()));
            } else {
              return "" + (d.getFullYear()) + "-" + (pad_zero(d.getMonth() + 1)) + "-" + (pad_zero(d.getDate())) + " " + (pad_zero(d.getHours())) + ":" + (pad_zero(d.getMinutes()));
            }
          },
          daterange_formatter: function(sd, ed, allday_p) {
            if (allday_p) {
              if (sd.getDate() !== ed.getDate() || sd.getMonth() !== ed.getMonth()) {
                return "" + (this.date_formatter(sd, allday_p)) + " - " + (pad_zero(ed.getMonth() + 1)) + "-" + (pad_zero(ed.getDate()));
              } else {
                return this.date_formatter(sd, allday_p);
              }
            } else {
              if (sd.getDate() !== ed.getDate() || sd.getMonth() !== ed.getMonth()) {
                return "" + (this.date_formatter(sd, allday_p)) + " - " + (pad_zero(ed.getMonth() + 1)) + "-" + (pad_zero(ed.getDate())) + " " + (pad_zero(ed.getHours())) + ":" + (pad_zero(ed.getMinutes()));
              } else if (sd.getHours() !== ed.getHours() || sd.getMinutes() !== ed.getMinutes()) {
                return "" + (this.date_formatter(sd, allday_p)) + " - " + (pad_zero(ed.getHours())) + ":" + (pad_zero(ed.getMinutes()));
              } else {
                return this.date_formatter(sd, allday_p);
              }
            }
          }
        };
    Last edited by stephen_; Dec 5, 2012 at 08:13. Reason: worked out the date syntax error...

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,718
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    You're needing to add a leading zero on to those values.

    A simple solution to that would be something like:

    Code javascript:
    function leadingZero(value) {
        return (value < 10) ? '0' + value : value;
    }
     
    {
        return "Date: " +
            leadingZero(d.getDate()) + "/" + leadingZero(d.getMonth() + 1) + "/" + d.getYear().toString().substr(-2) +
            " - Time " +
            leadingZero(d.getHours()) + ":" + leadingZero(d.getMinutes());
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •