Skip to content

Instantly share code, notes, and snippets.

@hectorAguero
Last active May 2, 2025 12:21
Show Gist options
  • Save hectorAguero/84030cbf19d0a3ff20c3de554c418b50 to your computer and use it in GitHub Desktop.
Save hectorAguero/84030cbf19d0a3ff20c3de554c418b50 to your computer and use it in GitHub Desktop.
Add 6 months to DateTime in Dart
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