Created
October 6, 2024 12:08
-
-
Save PlugFox/fcfe8b681e75280b1da32c50ee80b497 to your computer and use it in GitHub Desktop.
Padded date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:meta/meta.dart'; | |
void main() { | |
final now = DateTime.now(); | |
print(now.toIso8601String()); | |
print(Date.from(now).toIso8601String()); | |
print((Date.from(now) as DateTime).toIso8601String()); | |
} | |
extension type Date.from(DateTime _dt) implements DateTime { | |
Date(int year, int month, int day) : _dt = DateTime(year, month, day); | |
static String _numToPadString(num n, int p) => | |
n.toStringAsFixed(0).padLeft(p, '0'); | |
@redeclare | |
String toIso8601String() => '${_numToPadString(_dt.year, 4)}' | |
'-' | |
'${_numToPadString(_dt.month, 2)}' | |
'-' | |
'${_numToPadString(_dt.day, 2)}'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment