Results 1 to 5 of 5

Thread: JS - formatting a datetime value

Threaded View

  1. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,395

    Re: JS - formatting a datetime value

    Sorry to resurrect and older post, but this seems silly to me. I wonder if formatting date/time Strings was an oversight or intentionally left out because this seems like a basic function of a scripting language.

    Edit - Also, I tested this that works without having to use so many ternary if statements:
    Code:
    function yyyyMMdd_hhmmss(d) {
        var arrDate = d.toString().split(/ |:/);
        var yyyy = arrDate[3];
        var MM = ((d.getMonth() < 10) ? "0": "") + (d.getMonth()+1);
        var dd = arrDate[2];
        var hh = arrDate[4];
        var mm = arrDate[5];
        var ss = arrDate[6];
    
        return yyyy + MM + dd + '_' + hh + mm + ss;
    }
    Last edited by dday9; Jul 13th, 2017 at 05:56 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

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



Click Here to Expand Forum to Full Width