Vreme is a simple date and time formatter library, it formats date and time to match provided human-readable example. It is inspired by date formatting in Go lang and Stamp Ruby library.
Why? Because formating date and time should be as simple as posible and remembering mmm/MM/YYYY/%D formats is not.
"Vreme" means Time in Serbian.
Install from using npm:
npm install vreme
Install from using bower:
bower install vreme
Or download it on a Github.
Import Vreme and make a new instance, than you should simply provide date and human readable format.
If works with node.js and in the browser. It supports CommonJS, AMD and using as global variable.
You can format full dates and times or just a month/day name, as you can see in the example below:
var Vreme = require('Vreme')
var vreme = new Vreme()
var date = new Date('Sat, 01 Aug 2015 14:10:21')
// To get month
console.log(vreme.format(date, 'January')) // Output 'August'
console.log(vreme.format(date, 'Jan')) // Aug
console.log(vreme.format(date, '12')) // 08
console.log(vreme.format(date, '2')) // 8
// To convert day names
console.log(vreme.format(date, 'Monday')) // Saturday
console.log(vreme.format(date, 'monday')) // saturday
console.log(vreme.format(date, 'Mon')) // Sat
console.log(vreme.format(date, 'Mo')) // Sa
console.log(vreme.format(date, 'MO')) // SA
console.log(vreme.format(date, '1st')) // 1st
console.log(vreme.format(date, '22')) // 01
// To get year
console.log(vreme.format(date, '1999')) // 2015
console.log(vreme.format(date, '42')) // 15
// To get time
console.log(vreme.format(date, '4:10 pm')) // '2:10 pm'
console.log(vreme.format(date, '15:32')) // '14:10'
// To get full dates
console.log(vreme.format(date, 'March 25, 1999')) // August 01, 2015
console.log(vreme.format(date, 'March 1, 1999')) // August 1, 2015
console.log(vreme.format(date, 'March 25th')) // August 1st
console.log(vreme.format(date, '2014/04/25')) // 2015/08/01
console.log(vreme.format(date, '2014/04/25 4:10 am')) // 2015/08/01 2:10 pm
console.log(vreme.format(date, '02/03/11')) // 08/01/15
console.log(vreme.format(date, '21.04.2015')) // 01.08.2015
// Or you can combine it with text
console.log(vreme.format(date, 'Date: March, 25th')) // Date: August, 1st
If you pass
usePrototype: truein options Vreme will be added as a method of Date object (`Date#formatLike`), so you can use it like this:
var Vreme = require('Vreme')
var vreme = new Vreme({
usePrototype: true
})
var date = new Date('8/20/2015')
// Then you can get date like this
console.log(date.formatLike('March 25, 1999')) // August 20, 2015
Just include vreme.js or vreme.min.js in scripts and use the module same as described above.
<!doctype html>
<html>
<head>
<title>Vreme test</title>
</head>
<body>
<script src="vreme.min.js"></script>
<script>
var vreme = new Vreme();
var date = new Date();
console.log(vreme.format(date, 'Monday'));
</script>
</body>
</html>
This module is relying on regular expressions and I tried to keep it as simple as it's possible, so there is some limitations:
There are additional limitation that are not on this list, they'll be added.
For more complex date formats consider using strftime or moment.js
Use Githib issues to report a problem or suggest a new feature.
Vreme is written in ES6 and compiled with Babel.
Intall Babel by running npm i babel -g
then run npm install
to install dev dependencies and npm run compile
to transpile it to ES5.
If you want to run Babel manually, you'll need to do: babel src/index.js -o .tmp/vreme.js
and then buildify
to add support for CommonJS, AMD and script and to make minified version.
Run npm test
or mocha -R spec ./test/index.js
.