1.0.2 • Published 6 years ago

datentime v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

DateNTime

Simple way to format JavaScript Date Object.

DateNTime is small (1.3KB minified) but powerfull tool to work with date and time in JavaScript. It can be used both server and client side, DateNTime's tested and working in all modern browsers and even legacy browsers like IE9 and other browsers from same era.

Examples

console.log(DateNTime("Current date and time: !MM/!DD/!YYYY - HH:mm TD"));
// Current date and time: 09/12/2018 - 03:32 PM

console.log(DateNTime("!NW, !NM !DD, !YYYY", new Date("2017-04-25")));
// Tuesday, April 25, 2017

Localization is easy

Default names of months and days of the week are English but it's easy to add localized names in array as parameters.

var months_FR = ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];
var wdays_FR = ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"];

console.log(DateNTime("!NW, !NM !DD, !YYYY", new Date("2017-04-25"), months_FR, wdays_FR));
// Mardi, avril 25, 2017

Installing

Node.js

npm install datentime

Browser

Bundle / Inlcude datentime.min.js file in your project and you are good to go.

Syntax

Official syntax of DateNTime is following:

DateNTime(_format(String), _date(Date), _months(Array[12]), _wdays(Array[7]))

Although, this exact order of parameters isn't required. Script will automatically detect parameters by type and arange them itself. For example if you send parameters with following order, nothing will change, it will work as expected:

DateNTime(_wdays(Array[7]), _format(String))

Parameter Values

ParameterDescription
_format(String)(Optional) String with formatting. Default: !YYYY-!MM-!DD !hh:!mm:!ss.!SSS
_date(Date)(Optional) Date object. Default: Date object of current date new Date()
_months(Array[12])(Optional) Array of alternative/localized month names. Default: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
_wdays(Array[7])(Optional) Array of alternative/localized names for days of the week. Default: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

Format Characters

CharactersDescription
!UTMGet Unix Time in milliseconds
!UTSGet Unix Time in seconds
!YYYYGet full year
!YYGet last 2 digits of year
!MMGet month with leading zero 01-12
!MGet month 1-12
!NMGet name of the month January-December
!DDGet date with leading zero 01-31
!DGet date 1-31
!WGet day of the week 0-6 (where 0 is Sunday and 6 is Saturday)
!NWGet name of the day of the week Sunday-Saturday
!HHGet hour (12-hour format) with leading zero 12-01-11
!HGet hour (12-hour format) 12-1-11
!hhGet hour with leading zero 00-23
!hGet hour 0-23
!mmGet minutes with leading zero 00-59
!mGet minutes 0-59
!ssGet seconds with leading zero 00-59
!sGet seconds 0-59
!SSSGet milliseconds with leading zeros 000-999
!SGet milliseconds 0-999
!TDGet time of the day for 12-hour format AM/PM