Getting time and date on a file name

Discussion in 'JavaScript' started by chrisj, Aug 10, 2012.

  1. #1
    This javascript which I'm using works with a webcam to record a video.
    The end result, after the file is recorded, generates a file name, as you can see from the code below: Video+video_id.

    I recorded a file, and the script named the end-result file: Video1335806296490.flv. I understand that the time and date is in this file name, but it's hard to decipher what the time and date of the recording is.

    Can you help me with some code so as to make the the month, day, year and time appear more common and easily readable when a file name is generated? Here's the script I'm using currently:

    <CODE><script type="text/javascript">
    var video_id = get_id();
    var filename = get_parm('filename');
    if(filename == '' || filename == null)
    {
    filename = 'Video'+video_id;
    }

    var flashvars = {
    filename: filename,
    rtmpPath: "rtmp://xxxxxxxxxxxxxxx",
    finishURL: "videoplayer.htm?filename="+filename,
    };
    var params = {
    menu: "false",
    scale: "noScale",
    allowScriptAccess: "always",
    bgcolor: "#000000"
    };
    var attributes = {
    id:"webcamrecording"
    };
    swfobject.embedSWF("recording.swf", "recording", "620", "470", "9", "expressInstall.swf", flashvars, params, attributes);

    function get_id()
    {
    var newDate = new Date();
    return newDate.getTime();
    }

    function get_parm(name)
    {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
    {
    return "";
    }
    else
    {
    return results[1];
    }
    }

    </script></CODE>

    Someone suggested:

    "use iso string. it's WAY more human readable than unix time, and it sorts with a plain-text sort (no arg to [].sort())."

    <CODE>
    function get_id()
    {
    var newDate = new Date();
    return newDate.toISOString().split(".")[0].replace(/[^\dT]/g,"")
    }

    get_id()//=="20120724T201403" </CODE>

    But I don't know how or where to integrate it into my code.
    Can you provide some assistance? (or offer an alternative solution?)
    Thanks
     
    chrisj, Aug 10, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Change
    return newDate.getTime();
    (in your original get_id function) to
    return ' '+newDate.getMonth()+1+'-'+newDate.getDate()+'-'+newDate.getFullYear()

    Format it any way you like. The way shown will give you a filename in the form of "Video 8-10-2012.flv".
     
    Rukbat, Aug 10, 2012 IP
  3. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    Thanks so much for your assistance.

    I replaced
    return newDate.getTime();

    with

    return ' '+newDate.getMonth()+1+'-'+newDate.getDate()+'-'+newDate.getFullYear()

    and after testing, today (08-10-2012) the end result was a file named:

    Video 71-10-2012

    So, it's much closer to my goal, much appreciated.

    However, can you shed some light on why it resulted in 71-10-2012 instead of 08-10-2012 ?

    Thanks again. I look forward to any guidance.
     
    chrisj, Aug 10, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    The month runs from 0-11, so this month is 7. Some browsers see that code as '7 + '1', not 7 + 1. Change it to

    return ' '+parseInt(newDate.getMonth()+1)+'-'+newDate.getDate()+'-'+newDate.getFullYear()

    That'll force it to consider newDate.getMonth() and 1 as numbers, not strings.
     
    Rukbat, Aug 10, 2012 IP
  5. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Thanks for that. It was extremely helpful. Much appreciated.
    I then added +'-'+newDate.getTime() ant the line looks like this:

    return ' '+parseInt(newDate.getMonth()+1)+'-'+newDate.getDate()+'-'+newDate.getFullYear() +'-'+newDate.getTime()

    I added that to see if I could get see what time the video was created and saw the result time as 1346444193816.
    Is their any way to make the time to appear more common and easily readable when a file name is generated?

    Thanks again for any assistance.
     
    chrisj, Aug 31, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Try
    
    return ' '+parseInt(newDate.getMonth()+1)+'-'+newDate.getDate()+'-'+newDate.getFullYear() +' '+newDate.getHours() + ':' + newDate.getMinutes() + ':' + newDate.getSeconds();
    
    Code (markup):
     
    Rukbat, Aug 31, 2012 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #7
    I've found parseInt to be nothing but headaches, once you use it, JS wants to keep parsing integers.

    I'd use typecasting instead.

    
    	return ' ' +
    		Number(newDate.getMonth()+1) + '-' +
    		newDate.getDate() + '-' +
    		newDate.getFullYear() + ' ' +
    		newDate.getHours() + ':' +
    		newDate.getMinutes() + ':' +
    		newDate.getSeconds(); 
    
    Code (markup):
    Of course, a little formatting for clarity never hurt either :D
     
    deathshadow, Sep 1, 2012 IP
  8. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #8
    Never thought of that one. Cute. (And kind of elegant.)
     
    Rukbat, Sep 1, 2012 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    No, elegant would involve using WITH

    
    with (newDate) {
    	return ' ' +
    			Number(getMonth()+1) + '-' +
    			getDate() + '-' +
    			getFullYear() + ' ' +
    			getHours() + ':' +
    			getMinutes() + ':' +
    			getSeconds();
    }
    Code (markup):
    But naturally all the unenlightened wussies who learned programming after 2000 in 'college' are going to chime in about how 'with' is 'evil' or 'confusing' or some other whackjob asshattery about one of the most useful bits of C syntax languages -- one that exists to PROVIDE clarity and simplify code... the opposite it seems programmers of the past decade are being told about it. Naturally these are the same nutters who actually defend jQuery's (or even C's) needlessly cryptic syntax -- which is where the real laugh is. God forbid you have to type "document", but WITH is confusing...
     
    deathshadow, Sep 1, 2012 IP
  10. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #10
    As per usual, we agree. If there's more than 1 element I tend to use With - it makes the code cleaner and simpler. (Being evil is just an added bonus.)
     
    Rukbat, Sep 2, 2012 IP
  11. chrisj

    chrisj Well-Known Member

    Messages:
    606
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #11
    Thanks for all that help.
    My intention was to have a more informative file name when looking at the completed webcam video file list, and it is. But now also, for some reason, the file name appears across the bottom of the video itself (briefly). By looking at the code that I had posted, can you suggest how I can keep the file name, but without it appearing on the video?
    Any help will be appreciated. Thanks again.
     
    Last edited: Mar 5, 2013
    chrisj, Mar 5, 2013 IP
  12. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #12
    The code you posted doesn't add the file name to the video. That's probably being done by the code that's creating the video (which may be canned, so you have no control over what it does).
     
    Rukbat, Mar 5, 2013 IP