iLTC Home
Bookmark iLTC!

Categories

Perl
  • Perl Tutorials Beginners

  • Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

  • HTML
  • HTML Tutorials

  • Command Line
  • Command Line Tutorials

  • How Tos
  • Other Tips and Tricks



  • $koko =~ s/\./:)/g;
    1727900

    iLoveTheCode.com

    Formatting Time in Javascript

    Here's a few quick ways to format time, and then a long script for you to copy and paste.

    This is a little different than most of the iLTC site. The emphasis on this article is the end result for you to use, not the learning process. If you learn anything, hey, that's a bonus!

    The first two bits have links to their original articles. Skip what you don't want :)


    Epoch time in milliseconds (ko and ji are my made up variable names):
    ko = new Date();
    ji = ko.getTime();
    document.write(ji);

    Prints: 1141630966899

    Formatted time (hu is my made up variable name):
    hu = new Date();
    document.write(hu);

    Prints (Mac Firefox): Mon Mar 06 2006 02:42:46 GMT-0500 (EST)
    Prints (Windows IE): Mon Mar 6 02:42:46 EST 2006


    Time to get fancy (or jump to the copy and paste section :)
    -----

    GETTING THE DATE INTO CHUNKS:
    (we'll set time ahead and make cookie time with chunks)

    // get current date with built-in Date() method
    var hu = new Date();

    // convert to epoch and add a year in milliseconds with built-in getTime() method
    var ji = (hu.getTime() + (60 * 60 * 24 * 365 * 1000));

    // run new epoch back through built-in Date() method
    var ko = new Date(ji);

    // get pieces of date with built-in Date and Time methods
    var m=ko.getMonth();
    var d=ko.getDate();
    var y=ko.getYear();
    var w=ko.getDay();
    var hour=ko.getHours();
    var min=ko.getMinutes();
    var sec=ko.getSeconds();

    // convert returned month number to text
    if (m == 0) { m="Jan"; }
    else if (m == 1) { m="Feb"; }
    else if (m == 2) { m="Mar"; }
    else if (m == 3) { m="Apr"; }
    else if (m == 4) { m="May"; }
    else if (m == 5) { m="Jun"; }
    else if (m == 6) { m="Jul"; }
    else if (m == 7) { m="Aug"; }
    else if (m == 8) { m="Sep"; }
    else if (m == 9) { m="Oct"; }
    else if (m == 10) { m="Nov"; }
    else { m="Dec"; }

    // convert numbers to strings
    d = d + "";
    hour = hour + "";
    min = min + "";
    sec = sec + "";
    y = y + "";

    // add zeros to front of single digit variables.
    if (d.match(/^\d$/)) {
    d = "0" + d;
    }
    if (hour.match(/^\d$/)) {
    hour = "0" + hour;
    }
    if (min.match(/^\d$/)) {
    min = "0" + min;
    }
    if (sec.match(/^\d$/)) {
    sec = "0" + sec;
    }

    // adds 1900 to returned year if needed
    if (y.match(/^\d\d\d$/)) {
    // convert string back to number
    y = parseFloat(y);
    y = y + 1900;
    }

    // convert returned day number to text
    if (w == 0) { w="Sun"; }
    else if (w == 1) { w="Mon"; }
    else if (w == 2) { w="Tue"; }
    else if (w == 3) { w="Wed"; }
    else if (w == 4) { w="Thu"; }
    else if (w == 5) { w="Fri"; }
    else { w="Sat"; }

    // stores all the variables above in cookie format into var cooktime
    var cooktime = (w + ", " + d + "-" + m + "-" + y + " " + hour + ":" + min + ":" + sec + " GMT");

    // prints our pretty cookie time to the browser
    document.write(cooktime);


    NOTE: Our time is not really GMT (unless the user is in GMT). It's actually offset the number of hours the user is from GMT. I added the GMT to make the cookie gods happy.




    Free Copy and Paste Javascript Code:


    <script type="text/javascript">
    <!--

    var hu = new Date();
    var ji = (hu.getTime() + (60 * 60 * 24 * 365 * 1000));
    var ko = new Date(ji);

    var m=ko.getMonth();
    var d=ko.getDate();
    var y=ko.getYear();
    var w=ko.getDay();
    var hour=ko.getHours();
    var min=ko.getMinutes();
    var sec=ko.getSeconds();

    if (m == 0) { m="Jan"; }
    else if (m == 1) { m="Feb"; }
    else if (m == 2) { m="Mar"; }
    else if (m == 3) { m="Apr"; }
    else if (m == 4) { m="May"; }
    else if (m == 5) { m="Jun"; }
    else if (m == 6) { m="Jul"; }
    else if (m == 7) { m="Aug"; }
    else if (m == 8) { m="Sep"; }
    else if (m == 9) { m="Oct"; }
    else if (m == 10) { m="Nov"; }
    else { m="Dec"; }

    d = d + "";
    hour = hour + "";
    min = min + "";
    sec = sec + "";
    y = y + "";

    if (d.match(/^\d$/)) {
    d = "0" + d;
    }
    if (hour.match(/^\d$/)) {
    hour = "0" + hour;
    }
    if (min.match(/^\d$/)) {
    min = "0" + min;
    }
    if (sec.match(/^\d$/)) {
    sec = "0" + sec;
    }

    if (y.match(/^\d\d\d$/)) {
    y = parseFloat(y);
    y = y + 1900;
    }

    if (w == 0) { w="Sun"; }
    else if (w == 1) { w="Mon"; }
    else if (w == 2) { w="Tue"; }
    else if (w == 3) { w="Wed"; }
    else if (w == 4) { w="Thu"; }
    else if (w == 5) { w="Fri"; }
    else { w="Sat"; }

    var cooktime = (w + ", " + d + "-" + m + "-" + y + " " + hour + ":" + min + ":" + sec + " GMT");

    document.write(cooktime);

    -->
    </script>
    <br>
    Mon, 18-Feb-2036 05:43:38 GMT




    Resources:
    Getting Epoch Time in Javascript
    Get Formatted Time in Javascript
    Splitting an Array using Javascript
    Setting a Cookie using Javascript





    Previous Article: Get Formatted Time in Javascript
    Next Article: Substitute in Javascript Using replace Method






    Search iLTC w/ Google




    Recent Articles

  • HTML Font Code
  • Javascript and CSS - Easy Pop Up Help Balloons
  • Use OnMouseOver in Javascript
  • Use onMouseOut in Javascript
  • Use innerHTML to Change Text on a Page
  • Renaming a File with Perl's RENAME Function
  • PRINT to Browser with document.write Command in Javascript
  • Passing an Argument or Variable to a Javascript
  • Opening an Alert Box in Javascript
  • Opening a REMOTE WINDOW in Javascript
  • OPEN and WRITE a Data File in Perl
  • OPEN and READ a Data File in Perl
  • Mimic Typing from a Textfield in a Textarea with Javascript
  • Javascript Reset Form








  • Perl Programming - Javascript Programming - HTML Code - Mac Unix Command Line - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006-2009 iLoveTheCode.com


    Contact Chris | About this Site


    1727625
    Counter by iLoveTheCode.com


    Last Modified: Saturday, 10-Jan-2009 05:40:24 EST



    Hosted as well :)
    Home-brewed logo - 1997