简体   繁体   中英

Date Time format in Flutter

I have a function that needs parameter with type Date Time . For now, I'm using this.

DateTime.now().add(Duration(seconds: 5));

It means when the date and time in my app == DateTime.now() with delayed 5 seconds. It's will execute some function. But in my case, I need a specific date and time. For example, when date and time in my app == 2020-08-30(for the date will dynamically by day) 21:00:00 , it will execute some function. And the question is, how to make Date Time to a specific like 21:00:00 , so my function will execute every day with time is 21:00:00 ?

Short Answer

  1. Use Intl Package
  2. Use Customized DateFormat

截屏

Full Working Code

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: AddedTime(),
    );
  }
}

class AddedTime extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final timeFormat = new DateFormat('h:m:s a');

    final timeInString = "09:00:00 PM";

    var datetimeObject = timeFormat.parseStrict(timeInString);
    var newDatetimeObject = datetimeObject.add(Duration(seconds: 5));

    final now = DateTime.now();
    final dateFormat = new DateFormat('y/M/d');
    final completeFormat = new DateFormat('y/M/d h:m:s a');
    final todayInString = dateFormat.format(now);
    final completeString = "$todayInString $timeInString";

    var completeDatetimeObject = completeFormat.parseStrict(completeString);
    var newCompleteDatetimeObject = completeDatetimeObject.add(Duration(seconds: 5));

    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              "$timeInString",
            ),
            Text(
              "${datetimeObject.toString()}",
            ),
            Text(
              "${newDatetimeObject.toString()}",
            ),
            Text(
              "${completeDatetimeObject.toString()}",
            ),
            Text(
              "${newCompleteDatetimeObject.toString()}",
            ),
          ],
        ),
      ),
    );
  }
}

Step by step Tutorial

1. Install Dependencies

Add to your pubspec.yaml

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.16.1               <---------- Add this 

Run in your terminal

flutter pub get

2. Import Intl Package

import 'package:intl/intl.dart';

3. Create Date Format and use parseStrict

    final timeFormat = new DateFormat('h:m:s a');
    
    final timeInString = "09:00:00 PM";

    var datetimeObject = timeFormat.parseStrict(timeInString);
    var newDatetimeObject = datetimeObject.add(Duration(seconds: 5));

4. [Optional] If you need more precise date

we can utilize DateTime.now() function


    final now = DateTime.now();
    final dateFormat = new DateFormat('y/M/d');
    final completeFormat = new DateFormat('y/M/d h:m:s a');
    final todayInString = dateFormat.format(now);
    final completeString = "$todayInString $timeInString";

    var completeDatetimeObject = completeFormat.parseStrict(completeString);
    var newCompleteDatetimeObject = completeDatetimeObject.add(Duration(seconds: 5));

Consider using a Future instead

if(DateTime.now()==theTime){
             Future.delayed(Duration(seconds: 5)).then((value) => {
                 //Do excuse the function
               });
              }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM