Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

date-template

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

date-template

Easy function to return template date

  • 1.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
62
decreased by-36.08%
Maintainers
1
Weekly downloads
 
Created
Source

date-template

  1. Easy function of 20 lines of code
  2. Date output in any format
  3. Change the date type

install

$ npm install -D date-template

template

  • ~Y~ Year ****
  • ~M~ Month, from 01 to 12
  • ~D~ Day, from 01 to 31
  • ~Dw~ Day of the week 0-sunday, 6-saturday
  • ~h~ hourse
  • ~m~ minutes
  • ~s~ seconds
  • ~mm~ milliseconds

use

one parameter
var dateTemplate = require('date-template')

// return new Date

dateTemplate('~h~:~m~:~s~')
// "22:20:31"

dateTemplate('~h~/~m~/~s~')
// "22/20/31"

dateTemplate('hourse:~h~ minutes:~m~ seconds:~s~')
// "hourse:22 minutes:20 seconds:31"

dateTemplate('~h~:WOW:~m~')
// "22:WOW:20"
two parameters
var dateTemplate = require('date-template')

// return other Date

var oldDate = new Date()
oldDate.setHours(-3)

dateTemplate('~h~:~m~:~s~', oldDate)
// "19:20:31"

var milliseconds = +new Date()
// 1497382646504

dateTemplate('~h~:~m~:~s~', milliseconds)
// "22:39:03"
three parameters
var dateTemplate = require('date-template')

// return Date to user format

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
}

dateTemplate('Today is ~M~ ~D~, ~Y~', false, middleware)
// "Today is June 13, 2017"

source code

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
};

Keywords

FAQs

Package last updated on 13 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc