Skip to content

Instantly share code, notes, and snippets.

@JaveedIshaq
Last active April 24, 2022 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaveedIshaq/b6299aee059e861df29f2a47cbc6dda1 to your computer and use it in GitHub Desktop.
Save JaveedIshaq/b6299aee059e861df29f2a47cbc6dda1 to your computer and use it in GitHub Desktop.
How do I convert a date/time string to a DateTime object in Dart?
import 'package:flutter/material.dart';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: darkBlue,
),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
final parsedDate = DateTime.parse("1974-03-20 00:00:00.000");
@override
Widget build(BuildContext context) {
return Column(mainAxisAlignment: MainAxisAlignment.center, children: [
Text(
'Parse a String to DateTime object Flutter Dart',
style: Theme.of(context).textTheme.headline3,
),
const SizedBox(height: 50),
Text(
'$parsedDate\nis converted ${parsedDate.runtimeType} type of object',
style: Theme.of(context).textTheme.headline4,
),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment