Last active
June 25, 2017 22:12
-
-
Save SergProduction/e08907ddfc81d50e37c2bc03a461f281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// README and npm module https://www.npmjs.com/package/date-template | |
/** | |
*@function | |
*@name dateTemplate | |
*@param {string} format - ~Y~ ~M~ ~Dw~ ~D~ ~h~ ~m~ ~s~ ~mm~ | |
*@param {number} oldDate - milliseconds time OR Date object | |
*@param {function} middleware - Change the date type | |
*/ | |
const dateTemplate = function( format, oldDate, middleware ){ | |
let date = oldDate ? new Date(oldDate) : new Date(); | |
let tmp = { | |
"~Y~": date.getFullYear(), //Year **** | |
"~M~": date.getMonth() + 1, //Month, from 01 to 12 | |
"~D~": date.getDate(), //Day, from 01 to 31 | |
"~Dw~": date.getDay(), //Day of the week 0-sunday, 6-saturday | |
"~h~": date.getHours(), //hourse | |
"~m~": date.getMinutes(), //minutes | |
"~s~": date.getSeconds(), //seconds | |
"~mm~": date.getMilliseconds //milliseconds | |
}; | |
tmp = middleware ? middleware(tmp) : tmp | |
Object.keys(tmp).forEach( key => { | |
if( String(tmp[key]).length == 1 ) | |
tmp[key] = 0 + String(tmp[key]) | |
format = format.replace( key, tmp[key] ) | |
}) | |
return format | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dateTemplate = require('date-template') || dateTemplate | |
/* | |
*one parameter | |
*/ | |
console.log( dateTemplate('~h~:~m~:~s~') ) | |
// "22:20:31" | |
console.log( dateTemplate('~h~/~m~/~s~') ) | |
// "22/20/31" | |
console.log( dateTemplate('hourse:~h~ minutes:~m~ seconds:~s~') ) | |
// "hourse:22 minutes:20 seconds:31" | |
console.log( dateTemplate('~h~:WOW:~m~') ) | |
// "22:WOW:20" | |
/* | |
*two parameter | |
*/ | |
var oldDate = new Date() | |
oldDate.setHours(-3) | |
console.log( dateTemplate('~h~:~m~:~s~', oldDate) ) | |
// "19:20:31" | |
var milliseconds = +new Date() | |
// 1497382646504 | |
console.log( dateTemplate('~h~:~m~:~s~', milliseconds) ) | |
// "22:39:03" | |
/* | |
*three parameter | |
*/ | |
var middleware = (date) => { | |
var mounth = [ | |
'January', | |
'February', | |
'March', | |
'April', | |
'May', | |
'June', | |
'July', | |
'August', | |
'September', | |
'October', | |
'November', | |
'December' | |
] | |
date['~M~'] = mounth[ date['~M~']-1 ] | |
return date | |
} | |
console.log( dateTemplate('Today is ~M~ ~D~, ~Y~', false, middleware) ) | |
// "Today is June 13, 2017" |
this is simple)
dateTemplate('~h~:~m~') // 04:44
dateTemplate('hourse:~h~ minutes:~m~') // hourse:04 minutes:44
description https://www.npmjs.com/package/date-template
/**
* Example of JSDoc comment
* @param {string} name This is description of the parameter
* @param {number} age This is second parameter
* @return {boolean} This is return value
* @example
* // This is example of usage
* const isAllowed = checkAllow('Sova', 23)
*/
function checkAllow(name, age) {
if (name[0] === 'S' && age >= 18) return true
return false
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow! It's perfect! just not enough of example in jsfiddle