Created
June 4, 2025 09:27
-
-
Save md-riaz/1c02e9426ed35a137c00abab1e8cc365 to your computer and use it in GitHub Desktop.
flutter utc to local date conversion example
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'; | |
import 'package:intl/intl.dart'; | |
import 'package:timezone/data/latest.dart' as tz; | |
import 'package:timezone/timezone.dart' as tz; | |
void main() { | |
tz.initializeTimeZones(); | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Date Converter', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
), | |
home: const DateConverterPage(), | |
); | |
} | |
} | |
class DateConverterPage extends StatefulWidget { | |
const DateConverterPage({Key? key}) : super(key: key); | |
@override | |
_DateConverterPageState createState() => _DateConverterPageState(); | |
} | |
class _DateConverterPageState extends State<DateConverterPage> { | |
final TextEditingController _dateController = TextEditingController(); | |
final TextEditingController _epochController = TextEditingController(); | |
final TextEditingController _timezoneController = TextEditingController(); | |
String _convertedDate = ''; | |
void _convertDateFromDateTimeString() { | |
setState(() { | |
final timezoneName = _timezoneController.text; | |
final dateTimeString = _dateController.text; | |
if (timezoneName.isNotEmpty && dateTimeString.isNotEmpty) { | |
try { | |
final location = tz.getLocation(timezoneName); | |
final dateTime = DateTime.parse(dateTimeString); | |
final convertedDateTime = tz.TZDateTime.from(dateTime, location); | |
_convertedDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(convertedDateTime); | |
} catch (e) { | |
_convertedDate = 'Invalid Date or Timezone'; | |
} | |
} else { | |
_convertedDate = 'Please enter date, time, and timezone'; | |
} | |
}); | |
} | |
void _convertDateFromEpoch() { | |
setState(() { | |
final timezoneName = _timezoneController.text; | |
final epochString = _epochController.text; | |
if (timezoneName.isNotEmpty && epochString.isNotEmpty) { | |
try { | |
final location = tz.getLocation(timezoneName); | |
final epoch = int.parse(epochString); | |
final dateTime = DateTime.fromMillisecondsSinceEpoch(epoch * 1000, isUtc: true); | |
final convertedDateTime = tz.TZDateTime.from(dateTime, location); | |
_convertedDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(convertedDateTime); | |
} catch (e) { | |
_convertedDate = 'Invalid Epoch or Timezone'; | |
} | |
} else { | |
_convertedDate = 'Please enter epoch and timezone'; | |
} | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: const Text('Date Converter'), | |
), | |
body: Padding( | |
padding: const EdgeInsets.all(16.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.start, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
TextField( | |
controller: _dateController, | |
decoration: const InputDecoration( | |
border: OutlineInputBorder(), | |
labelText: 'Enter Date & Time (yyyy-MM-ddTHH:mm:ssZ)', | |
), | |
), | |
const SizedBox(height: 20), | |
ElevatedButton( | |
onPressed: _convertDateFromDateTimeString, | |
child: const Text('Convert from Date/Time'), | |
), | |
const SizedBox(height: 20), | |
TextField( | |
controller: _epochController, | |
decoration: const InputDecoration( | |
border: OutlineInputBorder(), | |
labelText: 'Enter Epoch Time (seconds)', | |
), | |
), | |
const SizedBox(height: 20), | |
ElevatedButton( | |
onPressed: _convertDateFromEpoch, | |
child: const Text('Convert from Epoch'), | |
), | |
const SizedBox(height: 20), | |
TextField( | |
controller: _timezoneController, | |
decoration: const InputDecoration( | |
border: OutlineInputBorder(), | |
labelText: 'Enter Timezone (e.g., Asia/Dhaka)', | |
), | |
), | |
const SizedBox(height: 20), | |
Text( | |
'Converted Date: $_convertedDate', | |
style: const TextStyle(fontSize: 18), | |
), | |
], | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment