suliatis / SimpleDateFormatJs

Java's SimpleDateFormat in JS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SimpleDateFormatJs

Overview

There are many JavaScript libraries out there to deal with date formating, but none of them has identical behavior with Java's SimpleDateFormat. It's a problem when you would like to use the same date formating patterns between your JavaScript frontend and Java backend. My main goal with this project is implement SimpleDateFormat's functionality in JavaScript, as far as possible also have fun and learn a lot.

Supported patterns

The list of supported patterns is not complete yet.

  • G: Era designator
  • y: Year Year
  • Y: Week year
  • M: Month in year (context sensitive)
  • L: Month in year (standalone form) warning: currently it works like the context sensitive format
  • w: Week in year
  • W: Week in month
  • D: Day in year
  • d: Day in month
  • F: Day of week in month
  • E: Day name in week
  • u: Day number of week (1 = Monday, ..., 7 = Sunday)
  • a: Am/pm marker
  • H: Hour in day
  • k: Hour in day (1-24)
  • K: Hour in am/pm (0-11)
  • h: Hour in am/pm (1-12)
  • m: Minute in hour
  • s: Second in minute
  • S: Millisecond
  • Z: Time zone (RFC 822)
  • X: Time zone (ISO 8601)

To read more about the patterns, please visit the offical documentation of SimpleDateFormat.

Examples

If no pattern defined in constructor it use pattern for default locale.

new SimpleDateFormat().format(new Date(2017, 4, 18)); //it prints 5/18/17 12:00 PM

You can apply a new pattern on existing date formatters.

var formatter = new SimpleDateFormat();
formatter.applyPattern("MM/dd/yy");
formatter.format(new Date(2017, 4, 18)); //it prints 05/18/17

It supports both native Date and moment.js.

new SimpleDateFormat("MM/dd/yy").format(new Date(2017, 4, 13)); //it prints: 05/13/17
new SimpleDateFormat("MM/dd/yy").format(moment([2017, 4, 13])); //same output

For more examples check out the tests in spec folder.

About

Java's SimpleDateFormat in JS.

License:Apache License 2.0


Languages

Language:JavaScript 100.0%