Dayparting
A Dayparting micro library to present time of day in Dayparts.
i.e. 8:00 AM => Early Morning
It follows standard dayparting conventions (read more about Dayparting on Wikipedia).
Example
Configure
In web browser
Include dayparting.js
like your any other JS file using script
tag in your page <head>
.
My Webpage ... ...
In Node Environment
Install dayparting
using npm
npm install dayparting --save-dev
Use require
to load Dayparting module.
var dayparting = ;
Use
Basic Usage
- Initialize:
Instantiate the
dayparting
with a locale configuration.
var myDaypart = ;
This will set en-US
as default locale for myDaypart
and use given localeJSON
, note that structure of localeJSON
follows ICU Message Syntax where keynames have to be same as given in the example. You can have strings for multiple locales within same localeJSON
like (and thus load external JSON file containing strings for multiple locales);
"en-US": "earlyMorning": "Early Morning" "lateMorning": "Late Morning" ... ... "primeTime": "Prime Time" "fr_FR": "earlyMorning": "Tôt le matin" "lateMorning": "Tard dans la matinée" ... ... "primeTime": "Prime Time"
- Call:
Daypart provides a method
for()
which acceptsdate
object as a parameter, and returns string representation for the time within that date object.
var date = ; date;date; myDatepart; // returns 'Afternoon'
Use with Databinding
- Initialize
During initialization, along with
locale
andlocaleJSON
, you can providebinding
to enable data-binding on elements, it isfalse
by default.
var myDaypart = ;
- Attaching Elements
Once
myDaypart
is initialized and you have DOM with existing elements as follows;
Will be aired at tomorrow 7:06 PM
Use attach()
to add elements which will be bound to Daypart.
myDaypart;
This will automatically set string for 7:06 PM
in the label with en-US
locale.
- Switch Locales
Once elements are attached with
daypart
object, simply callsetLocale
on the object and contents of all the attached elements will be updated automatically.
myDaypart;
Version Information
- 0.2.0 - Added support for one-way data-binding to enable live locale switch, bug fixes.
- 0.1.1 - Minor refactoring, README updated.
- 0.1.0 - First Release.