Getting starting time of the particular date in dart

1,517

Solution 1

Did you mean like this:

  DateTime toTime= new DateTime.now();
  DateTime fromTime= new DateTime(toTime.year, toTime.month, toTime.day);
  print(toTime);
  print(fromTime);

output:

2019-09-30 17:15:20.294
2019-09-30 00:00:00.000

Solution 2

Try this package, Jiffy it much simpler and is inspired by momentjs. It looks like you are trying to get the start day of toTime and set it to fromTime. Do the following

var toTime = Jiffy().dateTime; // This returns DateTime.now();
// or
var toTime = DateTime.now();

var fromTime = Jiffy(toTime).startOf(Units.DAY).dateTime; // This will return the start of day

print(toTime); // 2019-09-30 14:25:36.105
print(fromTime); // 2019-09-30 00:00:00.000
Share:
1,517
mark
Author by

mark

Updated on November 24, 2022

Comments

  • mark
    mark over 1 year

    Hi I am trying to get the start time as current date 12:00 AM as my fromTime

    DateTime toTime= new DateTime.now();
    DateTime fromTime= toTime.subtract(new Duration(days: 1));
    

    By above I am getting fromTime value as curent time - 24hrs, but I want fromTime as current date 12 AM

    output what I am getting is

    2019-09-30 14:25:36.105 //toTime
    2019-09-29 14:25:36.105 //fromTime
    

    expected output:

    2019-09-30 14:25:36
    2019-09-30 00:00:00
    

    help me out to get the above,Thanks.

  • mark
    mark over 4 years
    yes, I am not getting how to get that time, Thanks :)