Reputation: 996
How to convert the time to am/pm ?
I have this output
I/flutter (17720): 9:00:00
I/flutter (17720): 11:00:00
I/flutter (17720): 12:00:00
This is what I have tried
final item = snapshot.data[index];
print("Time " + item['time'].toString());
DateTime dateTime = DateTime.parse(item['time'].toString());
print(DateUtil().formattedTime(dateTime));
DateUtil
String formattedTime(DateTime dateTime) {
return DateFormat().add_jm().format(dateTime);
}
Error
I/flutter (17720): Time 09:00:00
════════ Exception caught by widgets library ═══════════════════════════════════ The following FormatException was thrown building Tags(dirty, state: TagsState#b3a2f): Invalid date format 09:00:00
Upvotes: 33
Views: 60439
Reputation: 555
Here is my solution a little simpler to understand I guess. And there is no need to add any library.
String getFormattedDateTime({d = "2022-11-15 14:21:37.152"}){
List<String> months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
List<String> days = [
'Monday',
'Tuseday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
];
final dateTimeObj = DateTime.parse(d);
// date format
String fdate = "${days[dateTimeObj.weekday - 1].substring(0, 3)}, ${months[dateTimeObj.month-1].substring(0, 3)}-${dateTimeObj.day}";
// time format
String time = "${(dateTimeObj.hour > 12 ? dateTimeObj.hour - 12 : dateTimeObj.hour).abs()}:${dateTimeObj.minute} ${dateTimeObj.hour >= 12 ? "PM" : "AM"}";
return "$fdate $time";
}
This function will return formatted String something like this Tus, Nov-15 4:54 PM again you can format it accordingly.
Upvotes: 0
Reputation: 296
You can use the intl library https://pub.dev/packages/intl and format your DateTime
DateFormat.yMEd().add_jms().format(DateTime.now());
Output:
'Thu, 5/23/2013 10:21:47 AM'
Upvotes: 20
Reputation: 458
this function can helps to convert only time, Without date
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
String formatedTime(TimeOfDay selectedTime) {
DateTime tempDate = DateFormat.Hms().parse(selectedTime.hour.toString() +
":" +
selectedTime.minute.toString() +
":" +
'0' +
":" +
'0');
var dateFormat = DateFormat("h:mm a");
return (dateFormat.format(tempDate));
}
Upvotes: 9
Reputation: 2065
DateFormat is for formatting and parsing dates in a locale-sensitive manner.
Convert Time
print(DateFormat.jm().format(DateFormat("hh:mm:ss").parse("14:15:00")));
Output : 3:20 AM
Convert Date
print(DateFormat('yyyy-MMMM-dd').format("2021-05-14 00:00:00.000"));
Output : 2021-May-14
Time Picker
Future<Null> _selectTime(BuildContext context) async {
final TimeOfDay picked = await showTimePicker(
context: context,
initialTime: _con.selectedTime,
);
if (picked != null) {
String selTime =
picked.hour.toString() + ':' + picked.minute.toString() + ':00';
print(DateFormat.jm().format(DateFormat("hh:mm:ss").parse(selTime)));
}}
Date Picker
_selectDate(BuildContext context) async {
final DateTime picked = await showDatePicker(
context: context,
initialDate: _con.selectDateAndTime,
firstDate: DateTime(2021),
lastDate: DateTime(2040),
);
if (picked != null) {
print(picked);
}}
Upvotes: 12
Reputation: 861
Use this code:
DateFormat('hh:mm a').format(DateTime.now());
According to the intl library, it states that a represents AM/PM.
Upvotes: 86