DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

DateTime: Simple Date Operations In Javascript

12.02.2005
| 69445 views |
  • submit to reddit
        
/*
### begin_: file metadata
    ### <region-file_info>
    ### main:
    ###   - name : cfDateTime.js
    ###     desc : |
    ###         Simple date operations in jscript.
    ###         This file is for use with windows scripting host.
    ###     date : created="Thu 2005-12-01 11:57:38"
    ###     last : lastmod="Thu 2005-12-01 12:18:57"
    ###     lang : jscript
    ###     tags : jscript javascript date time now month hour year cfDateTime
    ### </region-file_info>
    */

/// begin_: declare and init variables
    var today       = new Date();
    var strYear     = today.getFullYear();
    var iMonth      = today.getMonth() + 1; // +1, we do NOT want zero-based month index
    var iQuarter    = Math.ceil((iMonth / 12) * 4);
    var iDay        = today.getDate();
    var strDateOut  = "";

/// begin_: leading zeropad single-digit numbers
    iMonth = (iMonth < 10)? "0" + iMonth : iMonth;
    iDay = (iDay < 10)? "0" + iDay : iDay;

/// begin_: display output
    strDateOut = strYear+"-"+ iMonth +"-"+iDay + " ";
    WScript.Echo (strDateOut);