iLTC Home
Bookmark iLTC!
iLTC Google Boards

Google Groups
Receive The Code in Email!



Categories

Perl
  • Perl Tutorials Beginners

    Javascript
  • Javascript Tutorials
  • Javascript Tutorials Beginners

    HTML
  • HTML Tutorials



  • Perl with cheese...
    142,990

    iLoveTheCode.com
    Javascript > Javascript Tutorials How To Easy > Formatting Time in Javascript

    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



    Click HERE to comment or discuss at iLoveTheCode GOOGLE Group
    Previous Article: Get Formatted Time in Javascript
    Next Article: Substitute in Javascript Using replace Method





    jQuery in ActionjQuery in Action

    Bear Bibeault, Yeh...

    New $26.39

    Head First HTML with CSS...Head First HTML with CSS & XHTML

    Eric Freeman, Elis...

    New $23.99

    New Perspectives on Crea...New Perspectives on Creating Web Pag...

    Patrick Carey

    New $69.26

    CSSCSS

    David McFarland

    New $23.09

    Information Architecture...Information Architecture for the Wor...

    Louis Rosenfeld, P...

    New $26.39

    New Perspectives on HTML...New Perspectives on HTML, XHTML, and...

    Patrick Carey

    New $69.26

    CSS MasteryCSS Mastery

    Andy Budd, Simon C...

    New $23.85

    JavaScriptJavaScript

    David Flanagan

    New $31.49

    Bulletproof Web DesignBulletproof Web Design

    Dan Cederholm

    New $23.99


    Of Interest:
  • To See if a Directory Exists in Perl
  • Getting What is Matched in Perl
  • Use the Perl PRINT FUNCTION
  • Matching at the Beginning and End
  • Javascript Focus Form Field
  • Use onMouseOut in Javascript
  • Javascript Pop - Use Pop to Remove Items from the End of an Array in Javascript
  • Mimic Typing from a Textfield in a Textarea with Javascript
  • HTML Title Tag
  • HTML Frames
  • Make Italic Text Using HTML
  • HTML Center

  • More Articles:
  • Write a FOR LOOP in Perl
  • Encryption with the Crypt Function in Perl
  • Use the Perl PRINT FUNCTION
  • Matching with a Regular Expression, REGEX, in Perl
  • onClick Javascript
  • Reading Cookies using Javascript
  • Splitting an Array using Javascript
  • If, Else If, and Else in Javascript
  • Get String Length with Javascript
  • Make Strikethrough Text Using HTML
  • HTML Forms
  • HTML Codes
  • Make Text Bold Using HTML

  • Search iLTC w/ Google





    Recent Articles

  • Javascript Focus Form Field
  • Javascript Lowercase Text
  • Javascript Uppercase Text
  • Javascript Superscript Text
  • Javascript Subscript Text
  • Make Text Blink Using Javascript
  • Make a String into a Link in Javascript
  • Javascript Italics Text
  • Javascript Bold Text
  • Javascript Redirect
  • HTML Frames
  • HTML Color Chart
  • HTML Link
  • Javascript Status Bar








  • Perl Programming - Javascript Programming - Maybe Some PHP - Free Scripts - ILoveTheCode
    All Content © 2006 iLoveTheCode.com


    Contact Chris | About this Site


    439,755
    Counter by iLoveTheCode.com


    Last Modified: Friday, 23-Jun-2006 07:23:13 EDT