Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

DateTime: Simple date operations in javascript

/*
### 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);

simple templating engine

this will replace variables within a string that are denoted as [[variable]] and will replace it with the value of data[variable]


function parseTemplate(content, data) {
	content=String(content);
	re = /\[\[(\w*)\]\]/gi;

	return (
		content.replace(
			re, 
			function ($1,$2,$3) {
				return data[$2];
			}
		)
	);
}
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS