npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dateformat-converter

v2.0.1

Published

Package to convert between dateformats

Downloads

862

Readme

dateformat-converter

npm semantic-release Commitizen friendly Build Status codecov

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.

Getting Started

Prerequisites

In order to install this software, you will need:

  • Node version 6.*
  • npm version 3.* (comes with Node 6.*)

Installing

To install this package, run

npm install dateformat-converter

Project Setup for development:

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

Running the Tests

To run the project unit tests, run

npm test

To see the coverage results in HTML format, run

npm run test:results

Usage

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

<script src="dist/dateformat-converter.min.js"></script>
<script>
    var newDateFormat = dateformatConverter.convert('YYYY-MM-DD hh:mm:ss A', 'momentjs', 'jquery-dateFormat');
</script>

API

The library exports two methods

loadConfig

Arguments:

  • configurationName (String)
  • configurationObject (Object)

This method allows to register or replace a pattern. You need to provide the pattern name and its configuration.

convert(String, String, String)

Arguments:

  • valueToConvert (String)
  • sourceFormat (String)
  • destinationFormat (String)

This method performs the conversion of the valueToConvert, that is specified in the sourceFormat into the destinationFormat.

Configuration object

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: '<Post or ante meridiem (lower case)>',
	upperCaseMeridian: '<Post or ante meridiem (upper case)>',
	secondsSimple: '<Seconds without leading zero>',
	secondsLeading: '<Seconds with leading zero>',
	minutesSimple: '<Minutes without leading zero>',
	minutesLeading: '<Minutes with leading zero>',
	hoursSimple24Format: '<Hours in 24h format without leading zero>',
	hoursLeading24Format: '<Hours in 24h format with leading zero>',
	hoursSimple12Format: '<Hours in 12h format without leading zero>',
	hoursLeading12Format: '<Hours in 12h format with leading zero>',
	daysSimple: '<Day of month without leading zero>',
	daysLeading: '<Day of month with leading zero>',
	monthsSimple: '<Month number without leading zero>',
	monthsLeading: '<Month number with leading zero>',
	monthsSimpleText: '<Short month name>',
	monthsFullText: '<Long month name>',
	yearsTwoDigits: '<2 digit year>',
	yearsFourDigits: '<4 digit year>',
	unixTimestamp: '<Unix timestamp>',
	timezoneName: '<Offset from UTC>'
}

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

Built in patterns

Contributing

Issues and Pull Requests welcome!

Authors

  • João Marques - Initial work

License

This project is licensed under the MIT License - see the LICENSE file for details