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: Günter Zöchbauer

var newHour = 5;
time = time.toLocal();
time = new DateTime(time.year, time.month, time.day, newHour, time.minute, time.second, time.millisecond, time.microsecond);

There were discussions to add an update() method that allows to modify specific parts only, but it doesn't look like this has landed.


Solution 2: Jama Mohamed

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 3: A. Tratseuski

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 4: Jamie O'Toole

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 5: m123

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 6: Mohammad Reza Abbasniya

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 7: spydon

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 8: Mohammad Rameez

var newHour = 5; time = time.toLocal(); time = new DateTime(time.year, time.month, time.day, newHour, time.minute, time.second, time.millisecond, time.microsecond);


Solution 9: Adam Smaka

You can use Extension Methods with dart to extend existing libraries.

First define your extension , by extending the DateTime Object:

extension DateTimeExt on DateTime {
  DateTime applyTimeOfDay({required int hour, required int minute}) {
      return DateTime(year, month, day, hour, minute);
}
}

Once your new method is defined, you can call it exactly like the already defined methods of that object.

in this case, once you defined the applyTimeOfDay() method you can call it like so :

int myNewHour = 10
int myNewMinute = 8
var time = DateTime.parse("2018-08-16T11:00:00.000Z");
time = time.applyTimeOfDay(hour : myNewHour, minute : myNewMinute);