Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Created October 6, 2024 12:08
Show Gist options
  • Save PlugFox/fcfe8b681e75280b1da32c50ee80b497 to your computer and use it in GitHub Desktop.
Save PlugFox/fcfe8b681e75280b1da32c50ee80b497 to your computer and use it in GitHub Desktop.
Padded date
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