Code Monkey home page Code Monkey logo

time_formatter's Introduction

Time Formatter in Dart

This library takes UNIX (milliseconds) timestamps and converts them to pretty, human-readable time formats.

Using

Add the package to your pubspec.yaml

dependencies:
  time_formatter: ^1.0.0

Import the library in a .dart file

import 'package:time_formatter/time_formatter.dart';

Format an arbitrary UNIX timestamp (int type, milliseconds since epoch) with formatTime()

String formatted = formatTime(1548979724964)

Format

Here's a mini truth table for the format of the value returned by the function:

  • < 1 second : "Just now"
  • < 60 seconds : "X seconds" (2-59)
  • < 2 minutes : "1 minute"
  • < 60 minutes : "X minutes" (2-59)
  • < 2 hours : "1 hour"
  • < 24 hours : "X hours" (2-23)
  • < 2 days : "1 day"
  • < 7 days : "X days" (2-6)
  • < 2 weeks : "1 week"
  • < 28 days : "X weeks" (2-3)
  • < 30.44 * 1.5 days : "1 month"
  • < 365 - 15.22 days : "X months" (2-12)
  • < 730 days : "1 year"
  • Rest : "X years"

I decided to use truncate() for everything except months, as they can yield scenarios where something was stamped 59 days ago, but it still returns "1 month" (for example). Monthly timestamps are rounded to the nearest month.

Contributing

If you spot a bug or would like to provide additional functionality, you are more than welcome to submit an issue or pull request.

License

GPL 2.0, as per recommendation.

time_formatter's People

Contributors

tpscrpt avatar

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.