日付型の宣言と計算方法
flutter pub add age_calculator
DateTime birthday = DateTime.parse('1968-06-10');
DateTime selecteddate1 = DateTime.now();
DateTime selecteddate2 = DateTime.now().add(Duration(days: 30));
......
final age = AgeCalculator.age(birthday, today: selecteddate1);
final DateTime days14 = selecteddate1.add(Duration(days: 14));
final DateTime days90 = selecteddate1.add(Duration(days: 90));
final int daysselect = selecteddate2.difference(selecteddate1).inDays;
......
年齢計算その他色々
import 'package:age_calculator/age_calculator.dart';
void main() {
DateTime birthday = DateTime(1997, 3, 5);
DateDuration duration;
// 今日が 2021-03-08 の場合。。
duration = AgeCalculator.age(birthday);
print('Your age is $duration'); // Your age is Years: 24, Months: 0, Days: 3
final year = duration.years;
final month = duration.months;
final day = duration.days;
return '$year歳$monthヶ月$day日'; // 24歳0ヶ月3日
//Find out your age on any given date
duration = AgeCalculator.age(birthday, today: DateTime(2030, 5, 1));
print('Your age is $duration'); // Your age is Years: 33, Months: 1, Days: 26
// Find out when your next birthday will be at 2021-03-08
duration = AgeCalculator.timeToNextBirthday(birthday);
print('You next birthday will be in $duration');
// You next birthday will be in Years: 0, Months: 11, Days: 25
// Find out when your next birthday will be on any given date
duration = AgeCalculator.timeToNextBirthday(birthday,
fromDate: DateTime(2021, 3, 2));
print('You next birthday will be in $duration');
// You next birthday will be in Years: 0, Months: 0, Days: 3
// Find out the difference between two dates
duration = AgeCalculator.dateDifference(
fromDate: DateTime(2021, 1, 2),
toDate: DateTime(2025, 5, 2),
);
print('The difference is $duration');
// You next birthday will be in Years: 4, Months: 4, Days: 0
// Add time to any date
DateTime date = AgeCalculator.add(
date: DateTime(2021, 1, 2),
duration: DateDuration(years: 5, months: 2, days: 1));
print(date);
// 2026-03-03 00:00:00.000
}
DateTime型を日本語表記する関数
flutter pub add intl
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
.....
String japanDate(DateTime dt) {
const Locale("ja");
initializeDateFormatting("ja");
return DateFormat.yMMMd('ja').format(dt).toString() +
'(${DateFormat.E('ja').format(dt)})';
}
カレンダーから日付を選択するWidget
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
textTheme: TextTheme(
titleMedium: TextStyle(fontSize: 20),
bodyMedium: TextStyle(fontSize: 18),
),
),
......
DateTime birthday = DateTime.parse('1968-06-10');
......
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'生年月日:' + japanDate(birthday),
style: Theme.of(context).textTheme.bodyMedium,
),
ElevatedButton(
onPressed: () => _selectBirthDate(context),
child: const Text('選択'),
),
],
),
),
........
Future<void> _selectBirthDate(BuildContext context) async {
final DateTime? picked = await showDatePicker(
context: context,
initialDate: birthday,
firstDate: DateTime(DateTime.now().year - 100),
lastDate: DateTime(DateTime.now().year + 100),
);
和暦表示(未整理 年のみで判断している)
String convertYear(int year) {
if(year < 1868) {
return '明治より前です';
} else if(year < 1912) {
return '明治${year - 33 - 1900}年';
} else if(year < 1926) {
return '大正${year - 11 -1900}年';
} else if(year < 1989) {
return '昭和${year - 25 -1900}年';
} else if(year < 2019) {
return '平成${year + 12 -2000}年';
} else {
return '令和${year - 18 -2000}年';
}
}