How to convert DateTime into different timezones?

前端 未结 4 1455
囚心锁ツ
囚心锁ツ 2021-01-02 04:02

How to convert DateTime into different timezones? The DateTime class has two methods .toLocal() and .toUtc(). But if I want to display time in another time zone. How can I d

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 04:29

    I wrote a package for this. It's called Instant, and it can convert a DateTime in any given timezone worldwide. Take a detailed look at https://aditya-kishore.gitbook.io/instant/

    The basic usage for converting a DateTime to a timezone is very simple:

    //Assumes Instant is in your pubspec
    import 'package:instant/instant.dart';
    
    //Super Simple!
    DateTime myDT = DateTime.now(); //Current DateTime
    DateTime EastCoast = dateTimeToZone(zone: "EST", datetime: myDT); //DateTime in EST zone
    return EastCoast;
    

    This works with one line of code and minimal hassle.

提交回复
热议问题