Results 1 to 5 of 5

Thread: JS - formatting a datetime value

  1. #1

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    JS - formatting a datetime value

    I'm trying to format the current datetime into the following format: yyyyMMdd_hhmmss as part of a file download process (the file downloaded will get time stamped with the above value in the name).
    in the JS I have this:
    new Date().toString("yyyyMMdd_hhmmss");
    In the results I'm getting this: Fri_Jul_07_2017_113038_GMT-0400_(Eastern_Daylight_Time)

    Woaza! That's way more than I asked for! Bleh. Everything I've looked up uses one library or another. I'm working within the confines of an application, so adding another library isn't a viable option as I don't have the chance to include any thing.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: JS - formatting a datetime value

    I think in JS to get what you want you basically have to use all the individual functions e.g. Date. + getFullYear() , getMonth() , getDate(), getHours() , getMinutes() , getSeconds() and concatenate them all together.

    also for some unknown reason in JS you have to add 1 to the Month, and you will probably have to format the output of each of those functions to your required number of digits !
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: JS - formatting a datetime value

    Yeah, in the end, that's what I ended up doing... kind of a pain in the arse, but it got the job done. I just couldn't see the point of pulling in a whole library just for a single line like this.
    javascript Code:
    1. var currentdate = new Date();
    2.                 b = "samplefile_" + currentdate.getFullYear() +
    3.                                      ((currentdate.getMonth() < 10) ?"0":"") + (currentdate.getMonth()+1) +
    4.                                      ((currentdate.getDate() < 10) ?"0":"") +  currentdate.getDate() + "_" +
    5.                                      ((currentdate.getHours() < 10) ?"0":"") + currentdate.getHours() +
    6.                                      ((currentdate.getMinutes() < 10) ?"0":"") + currentdate.getMinutes() +
    7.                                      ((currentdate.getSeconds() < 10) ?"0":"") + currentdate.getSeconds() + ".txt"

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,660

    Re: JS - formatting a datetime value

    Yep you would have thought that something as simple as that would be built in a bit more elegantly and not make you rely on library's to have a nice way to do it.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,753

    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 | 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