all files / src/util/ dateConvert.ts

33.33% Statements 8/24
0% Branches 0/2
25% Functions 1/4
33.33% Lines 8/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                                           
/**
 * Created by gavorhes on 11/4/2015.
 */
 
import provide from './provide';
let nm = provide('util.dateConvert');
 
function leadingPad(inNum) {
    let strNum = inNum.toFixed();
    if (strNum.length < 2) {
        strNum = '0' + strNum;
    }
 
    return strNum;
}
 
nm.leadingPad = leadingPad;
 
/**
 * Given a date return a string in the format YYYY-mm-dd hh:MM:SS
 * @param {Date} dte to convert
 * @returns {string} the formatted date string
 */
export function dateToYyyyMmDdHhMmSs(dte: Date): string {
    let yr = dte.getFullYear();
    let month = leadingPad(dte.getMonth() + 1);
    let day = leadingPad(dte.getDate());
    let hrs = leadingPad(dte.getHours());
    let mns = leadingPad(dte.getMinutes());
    let secs = leadingPad(dte.getSeconds());
 
    return `${yr}-${month}-${day} ${hrs}:${mns}:${secs}`;
}
 
nm.dateToYyyyMmDdHhMmSs = dateToYyyyMmDdHhMmSs;
 
 
/**
 * Given a date return a string in the format YYYYmmdd_hh0000
 * @param {Date} dte the input date
 * @returns {string} the formatted date string
 */
export function dateToYyyyMmDdHh000(dte: Date): string {
 
    let yr = dte.getFullYear();
    let month = leadingPad(dte.getMonth() + 1);
    let day = leadingPad(dte.getDate());
    let hrs = leadingPad(dte.getHours());
 
    return `${yr}${month}${day}_${hrs}0000`;
}
 
nm.dateToYyyyMmDdHh000 = dateToYyyyMmDdHh000;