Last active
May 2, 2025 12:21
-
-
Save hectorAguero/84030cbf19d0a3ff20c3de554c418b50 to your computer and use it in GitHub Desktop.
Add 6 months to DateTime in Dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
//final date = DateTime.parse('2025-04-29'); | |
final date = DateTime.parse('2025-01-01'); | |
return MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: Container( | |
decoration: ShapeDecoration(shape: RoundedRectangleBorder()), | |
child: ListTile( | |
title: Text('1. ${add183Days(date)} \n2. ${addSixMonths(date)}'), | |
onTap: () {}, | |
), | |
), | |
), | |
), | |
); | |
} | |
} | |
DateTime add183Days(DateTime date) { | |
const sixMonthsInDays = 183; | |
final sixMonthsAhead = DateTime( | |
date.year, | |
date.month, | |
date.day + sixMonthsInDays, | |
); | |
return sixMonthsAhead; | |
} | |
DateTime addSixMonths(DateTime date) { | |
final sixMonthsAhead = DateTime(date.year, date.month + 6, date.day); | |
return sixMonthsAhead; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment