How can I get the current date (w/o hour and minutes)?

前端 未结 7 1583
攒了一身酷
攒了一身酷 2020-12-05 12:33

All I can see in the documentation is Datetime.now() but it returns the Timespan, I need just the date.

相关标签:
7条回答
  • 2020-12-05 13:08

    first go to pub.dev and get the intl package and add it to your project.

    DateFormat.yMMMMd().format(the date you want to render . but must have the type DateTime)

    0 讨论(0)
  • 2020-12-05 13:20

    If you prefer a more concise and single line format, based on Günter Zöchbauer's answer, you can also write:

    DateTime dateToday = DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day) ; 
    

    Though, it'll make 3 calls to DateTime.now(), the extra variable won't be required, especially if using with Dart ternary operator or inside Flutter UI code block.

    0 讨论(0)
  • 2020-12-05 13:22

    you can get the current date as

     final now = new DateTime.now();
     String formatter = DateFormat('yMd').format(now);// 28/03/2020
                
    

    Q. are you wondering how do you remember the date format(DateFormat('yMd'))?

    A.=>Then Flutter Docs is the answer

    The DateFormat class allows the user to choose from a set of standard date time formats as well as specify a customized pattern under certain locales.Taken directly from the docs

    ICU Name                   Skeleton
     --------                   --------
     DAY                          d
     ABBR_WEEKDAY                 E
     WEEKDAY                      EEEE
     ABBR_STANDALONE_MONTH        LLL
     STANDALONE_MONTH             LLLL
     NUM_MONTH                    M
     NUM_MONTH_DAY                Md
     NUM_MONTH_WEEKDAY_DAY        MEd
     ABBR_MONTH                   MMM
     ABBR_MONTH_DAY               MMMd
     ABBR_MONTH_WEEKDAY_DAY       MMMEd
     MONTH                        MMMM
     MONTH_DAY                    MMMMd
     MONTH_WEEKDAY_DAY            MMMMEEEEd
     ABBR_QUARTER                 QQQ
     QUARTER                      QQQQ
     YEAR                         y
     YEAR_NUM_MONTH               yM
     YEAR_NUM_MONTH_DAY           yMd
     YEAR_NUM_MONTH_WEEKDAY_DAY   yMEd
     YEAR_ABBR_MONTH              yMMM
     YEAR_ABBR_MONTH_DAY          yMMMd
     YEAR_ABBR_MONTH_WEEKDAY_DAY  yMMMEd
     YEAR_MONTH                   yMMMM
     YEAR_MONTH_DAY               yMMMMd
     YEAR_MONTH_WEEKDAY_DAY       yMMMMEEEEd
     YEAR_ABBR_QUARTER            yQQQ
     YEAR_QUARTER                 yQQQQ
     HOUR24                       H
     HOUR24_MINUTE                Hm
     HOUR24_MINUTE_SECOND         Hms
     HOUR                         j
     HOUR_MINUTE                  jm
     HOUR_MINUTE_SECOND           jms
     HOUR_MINUTE_GENERIC_TZ       jmv
     HOUR_MINUTE_TZ               jmz
     HOUR_GENERIC_TZ              jv
     HOUR_TZ                      jz
     MINUTE                       m
     MINUTE_SECOND                ms
     SECOND                       s
    
    /// Examples Using the US Locale:
    ///      Pattern                           Result
    ///      ----------------                  -------
             new DateFormat.yMd()             -> 7/10/1996
             new DateFormat('yMd')            -> 7/10/1996
             new DateFormat.yMMMMd('en_US')   -> July 10, 1996
             new DateFormat.jm()              -> 5:08 PM
             new DateFormat.yMd().add_jm()    -> 7/10/1996 5:08 PM
             new DateFormat.Hm()              -> 17:08 // force 24 hour time
    

    Hope now you can get any kind of date Format required using the above formats.

    0 讨论(0)
  • 2020-12-05 13:23

    Use the 'day' in DateTime.now()

    0 讨论(0)
  • 2020-12-05 13:29

    There's no class in the core libraries to model a date w/o time. You have to use new DateTime.now().

    Be aware that the date depends on the timezone: 2016-01-20 02:00:00 in Paris is the same instant as 2016-01-19 17:00:00 in Seattle but the day is not the same.

    0 讨论(0)
  • 2020-12-05 13:31

    Create a new date from now with only the parts you need:

    DateTime now = new DateTime.now();
    DateTime date = new DateTime(now.year, now.month, now.day);
    
    0 讨论(0)
提交回复
热议问题