Giter Site home page Giter Site logo

ryan-haskell / date-format Goto Github PK

View Code? Open in Web Editor NEW
51.0 3.0 27.0 77 KB

A reliable way to format dates and times in Elm.

Home Page: https://package.elm-lang.org/packages/ryan-haskell/date-format/latest

License: BSD 3-Clause "New" or "Revised" License

Elm 100.00%
elm date format moment

date-format's Introduction

date-format

A reliable way to format dates and times with Elm.

Build Status

Using the elm package

elm install ryan-haskell/date-format

What is date-format?

If you're coming from Javascript, you might have heard of MomentJS.

MomentJS is a great library for formatting dates!

date-format has similar formatting options as Moment, but it uses Elm's awesome type system to provide human readable names, and catch typos for you at compile time!

No need to remember the difference between mm and MM and M!

A quick example

import DateFormat
import Time exposing (Posix, Zone, utc)



-- Let's create a custom formatter we can use later:


ourFormatter : Zone -> Posix -> String
ourFormatter =
    DateFormat.format
        [ DateFormat.monthNameFull
        , DateFormat.text " "
        , DateFormat.dayOfMonthSuffix
        , DateFormat.text ", "
        , DateFormat.yearNumber
        ]



-- With our formatter, we can format any date as a string!


ourTimezone : Zone
ourTimezone =
    utc



-- 2018-05-20T19:18:24.911Z


ourPosixTime : Posix
ourPosixTime =
    Time.millisToPosix 1526843861289


ourPrettyDate : String
ourPrettyDate =
    ourFormatter ourTimezone ourPosixTime

Would make ourPrettyDate return:

"May 20th, 2018" : String

Want more examples?

I've created a few more examples in the examples/ folder for this repo.

Here's how you can try them out:

  1. git clone https://github.com/ryan-haskell/date-format

  2. cd date-format/examples

  3. elm reactor

  4. Go to http://localhost:8000

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.