Package to convert between dateformats
npm install dateformat-converter![npm]()




This tool attempts to solve the problem that exists when trying to use different libraries that require different datetime patterns. For example, when using momentjs for dealing with dates, but a plugin for rendering a datetimepicker, most likely these two libraries will have different datetime patterns, and the developer will need to manually convert between the two. The case gets even trickier if this pattern needs to be dynamic, according to, for example, the user locale.
#### Example
A component receives as an input a datetime pattern that follows the momentjs strucutre. It then renders a datetimepicker from bootstrap-datetimepicker. Since these two libraries use different datetime patterns, in order to use, for example, the momentjs pattern 'YYYY-MM-DD hh:mm:ss A' it would be necessary to convert it to 'yyyy-mm-dd HH:ii:ss P'. This is very unpractical and this tool aims at simplifying this task.
In order to install this software, you will need:
- Node version 6.\*
- npm version 3.\ (comes with Node 6.\)
To install this package, run
```
npm install dateformat-converter
To get a copy of this repository for local development, follow these steps
``
git clone https://github.com/tigermarques/dateformat-converter.git
cd dateformat-converter
npm run setup
To run the project unit tests, run
``
npm test
To see the coverage results in HTML format, run
``
npm run test:results
#### CommonJS
``
var dateformatConverter = require('dateformat-converter');
var newDateFormat = dateformatConverter.convert('YYYY-MM-DD hh:mm:ss A', 'momentjs', 'jquery-dateFormat');
#### AMD
``
require('dateformat-converter', function(dateformatConverter) {
var newDateFormat = dateformatConverter.convert('YYYY-MM-DD hh:mm:ss A', 'momentjs', 'jquery-dateFormat');
})
#### Browser
``
The library exports two methods
Arguments:
- configurationName (String)
- configurationObject (Object)
This method allows to register or replace a pattern. You need to provide the pattern name and its configuration.
Arguments:
- valueToConvert (String)
- sourceFormat (String)
- destinationFormat (String)
This method performs the conversion of the valueToConvert, that is specified in the sourceFormat into the destinationFormat.
If you choose to create your own pattern for translation, you need to pass in an object with a subset of the following properties:
``
{
lowerCaseMeridian: '
upperCaseMeridian: '
secondsSimple: '
secondsLeading: '
minutesSimple: '
minutesLeading: '
hoursSimple24Format: '
hoursLeading24Format: '
hoursSimple12Format: '
hoursLeading12Format: '
daysSimple: '
daysLeading: '
monthsSimple: '
monthsLeading: '
monthsSimpleText: '
monthsFullText: '
yearsTwoDigits: '<2 digit year>',
yearsFourDigits: '<4 digit year>',
unixTimestamp: '
timezoneName: '
}
For example, for momentjs, the configuration object looks like
```
{
lowerCaseMeridian: 'p',
upperCaseMeridian: 'A',
secondsSimple: 's',
secondsLeading: 'ss',
minutesSimple: 'm',
minutesLeading: 'mm',
hoursSimple24Format: 'H',
hoursLeading24Format: 'HH',
hoursSimple12Format: 'h',
hoursLeading12Format: 'hh',
daysSimple: 'D',
daysLeading: 'DD',
monthsSimple: 'M',
monthsLeading: 'MM',
monthsSimpleText: 'MMM',
monthsFullText: 'MMMM',
yearsTwoDigits: 'YY',
yearsFourDigits: 'YYYY',
unixTimestamp: 'X',
timezoneName: 'Z'
}
- momentjs
- bootstrap-datetime-picker
- jquery-dateFormat
Issues and Pull Requests welcome!
João Marques - Initial work*
This project is licensed under the MIT License - see the LICENSE file for details