ali262883
ali262883

Reputation: 369

Add hours or days to a timestamp in flutter

I have a timestamp which in which I want to add 48 hours or 2 days. How can I add?

enter image description here

Upvotes: 0

Views: 1700

Answers (2)

Ashok
Ashok

Reputation: 3611

void main() {
  var date1 = DateTime.utc(2020, 10, 28, 0, 0, 0);
  date1 = date1.add(Duration(days: 2));
  print(date1);
}

date1 = date1.add(Duration(days: 2)); //days

date1 = date1.add(Duration(hours: 48)); //hours

Upvotes: 2

Ravi Singh Lodhi
Ravi Singh Lodhi

Reputation: 2793

The DateTime instance can be created from two methods -

  • fromMillisecondsSinceEpoch
  • fromMicroSecondsSinceEpoch

If you are getting seconds in timestamp, then convert those seconds to milliseconds & create the DateTime object:

// Pass milliseconds to this constructor
DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(milliseconds);

// Add 2 days to the dateTime object
dateTime.add(Duration(days: 2));

Upvotes: 1

Related Questions