https://github.com/westy92/const-date-time
A drop-in replacement for Dart's DateTime class with a const constructor.
https://github.com/westy92/const-date-time
const constructor constructors dart dart-lang dart-library dart-package dart2 dartlang datetime flutter flutter-package flutter-plugin
Last synced: 3 months ago
JSON representation
A drop-in replacement for Dart's DateTime class with a const constructor.
- Host: GitHub
- URL: https://github.com/westy92/const-date-time
- Owner: westy92
- License: mit
- Created: 2022-10-28T01:27:12.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-28T22:57:08.000Z (9 months ago)
- Last Synced: 2025-04-09T16:05:09.546Z (6 months ago)
- Topics: const, constructor, constructors, dart, dart-lang, dart-library, dart-package, dart2, dartlang, datetime, flutter, flutter-package, flutter-plugin
- Language: Dart
- Homepage: https://pub.dev/packages/const_date_time
- Size: 213 KB
- Stars: 4
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# const_date_time
[](https://github.com/westy92/const-date-time/actions/workflows/github-actions.yml?query=branch%3Amain)
[](https://codecov.io/gh/westy92/const-date-time)
[](https://github.com/sponsors/westy92)A drop-in replacement for Dart's `DateTime` class with `const` constructors.
## Getting started
Install the package:
```bash
flutter pub add const_date_time
```or
```bash
dart pub add const_date_time
```## Usage
You can use a `ConstDateTime` anywhere a `DateTime` is expected. All major `DateTime` constructors have a `const` version.
```dart
import 'package:const_date_time/const_date_time.dart';// const constructors
final year = const ConstDateTime(2022);
final date = const ConstDateTime(2022, 10, 27);
final dateTime = const ConstDateTime(2022, 10, 27, 12, 34, 56, 789, 10);final yearUtc = const ConstDateTime.utc(2022);
final dateUtc = const ConstDateTime.utc(2022, 10, 27);
final dateTimeUtc = const ConstDateTime.utc(2022, 10, 27, 12, 34, 56, 789, 1011);final ms = const ConstDateTime.fromMillisecondsSinceEpoch(1666931562000);
final msUtc = const ConstDateTime.fromMillisecondsSinceEpoch(1666931562000, isUtc: true);final us = const ConstDateTime.fromMicrosecondsSinceEpoch(1666931562000000);
final usUtc = const ConstDateTime.fromMicrosecondsSinceEpoch(1666931562000000, isUtc: true);
```You can access the underlying `DateTime` object directly:
```dart
final cdt = const ConstDateTime(2022);
final DateTime dt = cdt.dateTime;
// other getters are available as well:
final int dtYear = dt.year;
final int dtWeekday = dt.weekday;
```Don't forget other `DateTime` methods too!
```dart
final constDateTime = const ConstDateTime(2022);constDateTime.add(Duration(minutes: 5));
constDateTime.toIso8601String();
```You can convert a `DateTime` to a `ConstDateTime`.
```dart
final dateTime = DateTime(2024);
final ConstDateTime constDateTime = dateTime.toConstDateTime();
```## Sponsor
Please consider [sponsoring my work](https://github.com/sponsors/westy92) to ensure this library receives the attention it deserves.
## License
const_date_time is released under the MIT License.