Flutter - Convert Date and Time with Different Timezones
Last Updated :
24 Apr, 2025
We all know every country has different time zones depending upon its location on Earth. So, We will discuss how to change the date time into that time zone by simply passing two arguments. We will convert the date time into PST time.
Step-by-Step Implementation
Step 1: We will simply create 1 flutter project with the command
Dart
We will get 1 boilerplate code in this project we will change that with 1 floating button on clicking the button we will change the timezone and also will show both the timezones and date time in the Text widget.
Step 2: Add this package
Dart
Step 3: Add the scaffold and column in the body in the first screen
Dart
import 'package:flutter/material.dart';
import 'package:instant/instant.dart';
// Flutter code sample for [Timezone change template].
void main() {
runApp(const TimezoneChangeTemplate());}
class TimezoneChangeTemplate extends StatelessWidget {
const TimezoneChangeTemplate({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Convert Timezone Template",
theme: ThemeData(
primaryColor:Colors.green,
appBarTheme: const AppBarTheme(backgroundColor: Colors.green)
),
home:const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Timezone Change'),
),
body:Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children:[
])
);
}
}
Step 4: Add 3 text 1 for normal timezone date time ,2nd for changing timezone in PDT and and for changing timezone in PST without date time variable.
Dart
Column(children:[
// Normal Datetime text
Text("Date Time Before:")
// Datetime text according to
// timezone entered or choosen
,Text("Date Time After changing in PDT:"),
// Datetime text according
// to timezone entered o
,Text("Date Time After changing in PST:"),
])
Step 5: Add texts with date time variable
Dart
Column(children:[
// Normal Datetime text
Text("Date Time Before: ${DateTime.now()}"),
// Datetime text according to PST timezone entered or choosen
Text("Date Time After changing: ${curDateTimeByZone(zone: "PST")}"),
// Datetime text according to PDT timezone
Text("Date Time After changing: ${curDateTimeByZone(zone: "PDT")}"),
])
Here is the explanation of this function
Function name: dateTimeToZone
It will convert any date time into timezone date
- zone :-Write standard time zone in Capital letter like IST,PST
- date time :- Date time variable which you want to change to another timezone
Sample Code:
Dart
void main() {
print(dateTimeToZone(zone: "PST", datetime: DateTime(2023)));
}
Function name: curDateTimeByZone
It will change current date time to timezone written in zone
zone: Write standard time zone in Capital letter like IST,PST
Sample Code:
Dart
// current DateTime in PST timezone
print(curDateTimeByZone(zone: "PST", ));
Function Name: formatTime
It format the time from date time object
- time: It will take date time object as it value.
- divider: It will change the divider between hours, minutes ,seconds.Default is :.
- format: It will change the format of time. Default is HHMMSS.
Sample Code:
Dart
print(formatTime(time: SanFran,
divider: "/",
format: "HHMM",
is24hr: true
)); // prints format time in PDT
Function Name: formatDate
It format the date from date time object
- time : It will take date time object as it value.
- divider: It will change the divider between year, month ,date.Default is /.
- format: It will change the format of time. Default is MMDDYYYY.
Sample Code:
Dart
var SanFran =
curDateTimeByZone(zone: 'PDT');
print(formatDate(date: SanFran,
divider: "/",
format: "DDMMYYYY"
)); // prints format date in PDT
Complete Code
Dart
import 'package:flutter/material.dart';
import 'package:instant/instant.dart';
// Flutter code sample for [Timezone change template].
void main() {
print(dateTimeToZone(zone: "PST", datetime: DateTime(2023)));
print(curDateTimeByZone(zone: "PST", ));
var SanFran =
curDateTimeByZone(zone: 'PDT');
print(formatTime(time: SanFran,
divider: "/",
format: "HHMM",
is24hr: true
)); // prints format time in PDT
print(formatDate(date: SanFran,
divider: "/",
format: "DDMMYYYY"
)); // prints format date in PDT
runApp(const TimezoneChangeTemplate());}
class TimezoneChangeTemplate extends StatelessWidget {
const TimezoneChangeTemplate({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Convert Timezone Template",
theme: ThemeData(
primaryColor:Colors.green,
appBarTheme: const AppBarTheme(backgroundColor: Colors.green)
),
home:const MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Timezone Change'),
),
body:Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children:[
Text("Date Time Before: ${DateTime.now()}"),//Normal Datetime text
Text("Date Time After changing in PST: ${curDateTimeByZone(zone: "PST")}"),
Text("Date Time After changing in PDT: ${curDateTimeByZone(zone: "PDT")}"),
])
);
}
}
Output:

Similar Reads
Flutter - Convert Formatted String into DateTime Object
In Dart, there is 1 datatype called DateTime which refers to date and time. It has multiple inbuilt functions which provide us with various details like current date and time. Today we will learn how to convert a formatted string into a DateTime object of dart. For our convenience, we are changing t
2 min read
Flutter - Set MIN and MAX Selectable Dates in DatePicker
In Flutter, a DatePicker is a widget that provides a user interface for selecting dates. It allows users to choose a specific date from a calendar-like interface. Flutter doesn't have a built-in DatePicker widget, but you can create a date picker using the showDatePicker function, which displays a d
5 min read
Flutter - Find the Number of Days Between Two Dates
In this article, we will see how to get the difference between the two dates and display them in Flutter. Below is the step-by-step implementation. How to Use?final date1 = DateTime(2022, 4, 11); final date2 = DateTime.now(); late var difference; int daysBetween(DateTime from, DateTime to) { from =
3 min read
Flutter - Creating Time Picker Using Material Design
Time Picker is a UI component that controls or lets the selection of time in either 24-hours format or AM/PM format. Mainly is job is to ease out the process of time selection and to ensure that the user is picking or selecting a valid time in the application. This can be used in the applications su
7 min read
Application Localization in Flutter
In mobile applications, Localization is the process of adapting an application to support multiple languages and regional settings. This allows your app to be more accessible to users from different parts of the world. In Flutter, for implementing Localization flutter_localization is a package used
4 min read
Dart - Date and Time
A DateTime object is a point in time. The time zone is either UTC or the local time zone. Accurate date-time handling is required in almost every data context. Dart has the marvelous built-in classes for date time and duration in dart:core. Key Uses of DateTime in Dart:Comparing and Calculating Date
3 min read
Format Dates in Flutter
The Dart in-built method, for formatting, dates in Flutter according to the requirements is very limited and restrictive. While dealing with dates it should be in human-readable format but unfortunately, there's no way of formatting dates in flutter unless you make use of a third-party package.In th
3 min read
How to Take Date of Birth From User Manually in Flutter?
dob_input_field package helps you to take the date of birth from the user manually. This package validates the user inputted date of birth automatically i.e., auto validation. Many Applications need to input the date of birth from the user and then explicitly validate the data which is complicated.
2 min read
Day Night Time Picker in Flutter
day_night_time_picker is a Day Night Time Picker for flutter, beautiful day and night animation with the sun and moon assets. we don't need to add explicit assets. A sample video is given below to get an idea about what we are going to do in this article. Properties Some properties of day_night_time
3 min read
Integrating Maps and Geolocation Services Flutter
In today's Android application, incorporating maps and geolocation services is essential. Whether it be finding the user's location, finding nearby travel places, or providing navigation, these features might really enhance the user experience and ease it down. Flutter, Google's UI toolkit for build
6 min read