date object flutter code example
Example 1: flutter get current date
import 'package:intl/intl.dart';
main() {
static final DateTime now = DateTime.now();
static final DateFormat formatter = DateFormat('yyyy-MM-dd');
final String formatted = formatter.format(now);
print(formatted);
}
Example 2: flutter date time
var now = new DateTime.now();
var moonLanding = DateTime.parse(now);
moonLanding.year;
moonLanding.hour;
moonLanding.month;
moonLanding.year.toString() + '-' + moonLanding.month.toString() + '-' + moonLanding.day.toString()
############################ OR ###########################
import 'package:intl/intl.dart';
var dt = DateTime.now();
var newFormat = DateFormat("yy-MM-dd");
String updatedDt = newFormat.format(dt);
print(updatedDt);
var newDt = DateFormat.yMMMEd().format(dt);
print(newDt);
reference :
https:
Example 3: flutter date with timezone
static String formatISOTime(DateTime date) {
var duration = date.timeZoneOffset;
if (duration.isNegative)
return (date.toIso8601String() + "-${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
else
return (date.toIso8601String() + "+${duration.inHours.toString().padLeft(2, '0')}:${(duration.inMinutes - (duration.inHours * 60)).toString().padLeft(2, '0')}");
}