Time Stamper ClassAppend a time prefix or suffix to FileNames
File Name : TimeStamperCLS.js Requirement : none Author : Branimir Petrovic Submitted : 19/09/2002 Category : Class To use a class, follow this syntax : var MyObjClass = new MyClass; WScript.Echo(MyObjClass.MyMethod()); // FileName: TimeStamperCLS.js ////////////////////////////////////////////////////////////////////// if (WScript.ScriptName == "TimeStamperCLS.js") demo_TimeStamperCLS(); // ======================== CLASS ==================================== function TimeStamperCLS() { // Author: Branimir Petrovic // Date: 9 January 2002 // Version: 1.1.0 // // Revision History: // 9 January 2002 V 1.0.0 // 13 January 2002 V 1.1.0 Added getVersion() method // // // This class is used to produce prefix or suffix to be appended // before or after file name (for one of many situations where files // differ only by a date and time stamp embedded in the file name). // // Methods: // - getClassName() // - getVersion() // Returns version string "1.1.0" // - getNow([oDate]) // - getYYYYMMDD([oDate]) // - getHHMMSS([oDate]) // - getFullAsPostfix([oDate]) // - getFullAsPrefix([oDate]) // // Note: // All methods accept object of type Date in which case time stamps // are based on this object's value. If methods are called without // any param, then each call will create new Date object and return // its "stringified" value. // --- Private Data Members --- var SELF = "TimeStamperCLS"; var VERSION = "1.1.0"; var oDate; // --- Public Methods --- this.getClassName = function () {return SELF; } this.getVersion = function () { return VERSION; } this.getNow = getNow; this.getYYYYMMDD = getYYYYMMDD; this.getHHMMSS = getHHMMSS; this.getFullAsPostfix = getFullAsPostfix; this.getFullAsPrefix = getFullAsPrefix; // ---------------- Functions Implementing Public Methods ------- function getNow(oD) { // duplicates VBScript's getNow() functionality, returns string if (isDate(oD)) oDate = oD; else oDate = new Date(); var sNow = padWithZero(oDate.getDate()); sNow += " " + toNamedMonth(oDate.getMonth()+1); sNow += " " + oDate.getFullYear() + " "; sNow += padWithZero(oDate.getHours()) + ":"; sNow += padWithZero(oDate.getMinutes()) + ":"; sNow += padWithZero(oDate.getSeconds()); return sNow; // >>> } // -------------------------------------------------------------- function getYYYYMMDD(oD) { if (isDate(oD)) oDate = oD; else oDate = new Date(); var sNow = oDate.getFullYear(); sNow += padWithZero(oDate.getMonth()+1); sNow += padWithZero(oDate.getDate()); return sNow; } // -------------------------------------------------------------- function getHHMMSS(oD) { if (isDate(oD)) oDate = oD; else oDate = new Date(); var sNow = padWithZero(oDate.getHours()); sNow += padWithZero(oDate.getMinutes()); sNow += padWithZero(oDate.getSeconds()); return sNow; } // -------------------------------------------------------------- function getFullAsPostfix(oD) { // returns date as _20010808_174905 if (isDate(oD)) oDate = oD; else oDate = new Date(); return "_" + getYYYYMMDD(oDate) + "_" + getHHMMSS(oDate); } // -------------------------------------------------------------- function getFullAsPrefix(oD) { // returns date as 20010808_174905_ if (isDate(oD)) oDate = oD; else oDate = new Date(); return getYYYYMMDD(oDate) + "_" + getHHMMSS(oDate) + "_"; } // ---------------- Private helper functions -------------------- function toNamedMonth(iMonth) { switch (iMonth) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "Jun"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; default: return iMonth; } } // -------------------------------------------------------------- function padWithZero(iNum) { if (iNum<10) iNum = "0" + iNum; return iNum.toString(); } // -------------------------------------------------------------- function isDate(oDate) { try { return oDate.constructor==Date; } catch(e) { return false; } } } // ======================== END OF CLASS ========================= // ================================================================== function demo_TimeStamperCLS() { var oTS = new TimeStamperCLS(); var sMsg = "oTS.getNow() -> \t\t" + oTS.getNow() + "\n"; sMsg += "oTS.getYYYYMMDD() -> \t" + oTS.getYYYYMMDD() + "\n"; sMsg += "oTS.getHHMMSS() -> \t" + oTS.getHHMMSS() + "\n"; sMsg += "oTS.getFullAsPostfix() -> \t" + oTS.getFullAsPostfix() + "\n"; sMsg += "oTS.getFullAsPrefix() -> \t" + oTS.getFullAsPrefix() + "\n"; WScript.Echo(sMsg); } // ================================================================== |
|||||
![]()
|
|