Coder Social home page Coder Social logo

date_time's Introduction

pub package codecov likes style: lint Dart

date_time

Package to work with Date & Time in separation and with its ranges.

This project will be useful for projects that are related to booking.

Features

  • Only Date comparison
  • Only Time comparison
  • Overflowed Time with keeping days
  • Find crossing of dates
  • Find crossing of times in the day

Getting started

  1. Add dependency
dependencies:
  date_time: <newest>
  1. Import the dependency
import 'package:date_time/date_time.dart';

Usage

Please, check (examples) folder for more advanced examples.

Date

// Get [Date] & [Time] from [DateTime]
print(DateTime(2022, 1, 6).date); // prints 1/6/2022
print(DateTime(7, 38, 24).time); // 07:38:24
given('DateTime', () {
  final dateTime = DateTime(2020, 11, 30, 14, 33, 17);

  then('[Date] should be equal to', () {
    dateTime.date.should.be(Date(year: 2020, month: 11, day: 30));
  });

  then('[Time] should be equal to', () {
    dateTime.time.should.be(Time(hour: 14, minute: 33, second: 17));
  });
});

CopyWith

final date = Date(year: 2021, month: 3, day: 7);
print(date.copyWith(year: 2022)); // prints 07/03/2022

DateRange

final range = DateRange(
  const Date(year: 2021, month: 1, day: 1),
  const Date(year: 2021, month: 12, day: 31),
);

test('should be valid', () {
  range.isValid.should.beTrue();
});

Time

final time2 = time.addMinutes(30);
final isTime2After = time2 > time;
final isTime2After2 = time2.isAfter(time);
print('Is time2 after: $isTime2After');

CopyWith

  final time = Time(hour: 6, minute: 30, second: 7);
  print(time);                      // prints 06:30:07
  print(time.copyWith(second: 0));  // prints 06:30:00

Overflowed Time

to keep days

final time = Time(hour: 20).addHours(5);

print(time is OverflowedTime); // prints `true`
print(time.asOverflowed.days); // prints `1`

TimeRange

// TimeRange crossing
final timeRange = TimeRange(Time.now, Time.now.addHours(6));
final timeRange2 = TimeRange(Time.now.addHours(3), Time.now.addHours(9));

final isCrossing = timeRange.isCross(timeRange2);
print('Time ranges are crossing: $isCrossing');

Override time with clock package

withClock(
    Clock(() => DateTime.now().subtract(Duration(days: 10, minutes: 214))),
    () {
        print(clock.now());                    // 2022-06-21 16:28:46.366887
        print(DateTime.now());                 // 2022-07-01 20:02:46.367579    
        print('${Date.now()} ${Time.now()}');  // 6/21/2022 16:28:46                  
    },
);

Changelog

Please see the Changelog page to know what's recently changed.

Contributing

Feel free to contribute to this project.

If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue.
If you fixed a bug or implemented a new feature, please send a pull request.

We accept the following contributions:

  • New features
  • Improving documentation
  • Fixing bugs

Maintainers

Support me

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.