'Dart - How to set the hour and minute of DateTime object
How do I set/change the hour and/or minute of a DateTime object. Similar to Date.setHours(..) in JavaScript.
e.g If i did
var time = DateTime.parse("2018-08-16T11:00:00.000Z");
how do I set the hour and minute of time
Solution 1:[1]
now with extension u could do something like this
extension MyDateUtils on DateTime {
DateTime copyWith({
int? year,
int? month,
int? day,
int? hour,
int? minute,
int? second,
int? millisecond,
int? microsecond,
}) {
return DateTime(
year ?? this.year,
month ?? this.month,
day ?? this.day,
hour ?? this.hour,
minute ?? this.minute,
second ?? this.second,
millisecond ?? this.millisecond,
microsecond ?? this.microsecond,
);
}
}
Solution 2:[2]
I got a simpler solution:
DateTime newDate = DateTime.now();
DateTime formatedDate = newDate.subtract(Duration(hours: newDate.hour, minutes: newDate.minute, seconds: newDate.second, milliseconds: newDate.millisecond, microseconds: newDate.microsecond));
Then the XX:XX from 'formatedDate' should be 00:00
Explanation:
formatedDate is a new DateTime variable with the content of newDate minus hours, minutes... from it
Solution 3:[3]
I believe this is a better solution than the accepted answer:
DateTime dateTime = DateFormat('dd-MM-yyyy h:mm:ssa', 'en_US').parseLoose('01-11-2020 2:00:00AM');
Solution 4:[4]
You can also do this by adding or subtracting to DateTime, using this package Jiffy. It also respects leap years and how many days are in each month
var updateTime = Jiffy("2018-08-16T11:00:00.000Z").add(hours: 1); // 2018-08-16 12:00:00.000Z
print(updateTime.dateTime); // 2018-08-16 12:30:00.000Z
// also you can format it
print(updateTime.format("yyyy, MMM")); // 2018, Aug
// or use default formats
print(updateTime.yMMMEdjm); // Thu, Aug 16, 2018 12:30 PM
Solution 5:[5]
I had some issues while using the formatted form of a DateTime object (using DateFormat) in my widget because showDatePicker brought a new instance of DateTime and calling setState doesn't have effect on the view.
There might be better ways to comply with my problem, but I decided to create my own one which is updateable using .update method.
import 'package:flutter/material.dart';
class MDateTime {
int year, month, day, hour, min, sec;
factory MDateTime.fromDateTime(DateTime dateTime) {
return MDateTime(
dateTime.year,
dateTime.month,
dateTime.day,
dateTime.hour,
dateTime.minute,
dateTime.second,
);
}
MDateTime([
this.year = 0,
this.month = 0,
this.day = 0,
this.hour = 0,
this.min = 0,
this.sec = 0,
]);
void update({
int? newYear,
int? newMonth,
int? newDay,
int? newHour,
int? newMin,
int? newSec,
}) {
year = newYear ?? year;
month = newMonth ?? month;
day = newDay ?? day;
hour = newHour ?? hour;
min = newMin ?? min;
sec = newSec ?? sec;
}
DateTime toDateTime() {
return DateTime(year, month, day, hour, min, sec);
}
TimeOfDay toTimeOfDay() {
return TimeOfDay(hour :hour, minute: min);
}
String formatDate({String divider = "-"}){
return "$year$divider$month$divider$day";
}
String formatTime({String divider = ":"}){
return "$hour$divider$min$divider$sec";
}
}
Moreover, there is no need to use another package for date-time formatting.
Solution 6:[6]
You can remove the current hours and minutes, and then add your wanted hours and minutes.
So if you would want to set the time to for example 23:59 you could do this:
final date = DateTime.now();
date
..subtract(Duration(hours: date.hour, minutes: date.minute))
..add(Duration(hours: 23, minutes: 59));
(Note that the other time units like seconds and milliseconds aren't changed here)
Solution 7:[7]
var newHour = 5; time = time.toLocal(); time = new DateTime(time.year, time.month, time.day, newHour, time.minute, time.second, time.millisecond, time.microsecond);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | M123 |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | Mohammad Reza Abbasniya |
| Solution 6 | spydon |
| Solution 7 | Mohammad Rameez |
