147

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

Rap
  • 6,851
  • 3
  • 50
  • 88
user2070369
  • 3,970
  • 6
  • 22
  • 32

15 Answers15

232

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);

Hint: "new" is optional in Dart since quite a while

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
74

If you want only the date without the timestamp. You can take the help of intl package.

main() {
    var now = new DateTime.now();
    var formatter = new DateFormat('yyyy-MM-dd');
    String formattedDate = formatter.format(now);
    print(formattedDate); // 2016-01-25
} 

This requires the intl package:

dependencies:
  intl: ^0.16.1

And finally import:

import 'package:intl/intl.dart';
Magiczne
  • 1,586
  • 2
  • 15
  • 23
Eternalcode
  • 2,153
  • 3
  • 19
  • 28
49

You can get the current date using the DateTime class and format the Date using the DateFormat. The DateFormat class requires you to import the intl package so

add to pubspec.yaml

dependencies:
  intl: ^0.17.0

and import

import 'package:intl/intl.dart';

and then format using

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

In case you are wondering How do you remember the date format(DateFormat('yMd'))? 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. The below formats are taken directly from the docs

/// 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

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

Hope this helps you to get Date in any format.

Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131
14

With dart extension

extension MyDateExtension on DateTime {
  DateTime getDateOnly(){
    return DateTime(this.year, this.month, this.day);
  }
}

Usage:

DateTime now = DateTime.now(); // 30/09/2021 15:54:30
DateTime dateOnly = now.getDateOnly(); // 30/09/2021
function1983
  • 971
  • 11
  • 14
  • 1
    Excellent, I was looking exactly for this. Personally, I would name this extension just "date". Also, it can be a property. – Matej Hlatky Apr 28 '23 at 17:39
11

this without using any package (it will convert to string)

  DateTime dateToday =new DateTime.now(); 
  String date = dateToday.toString().substring(0,10);
  print(date); // 2021-06-24
Zaid Salah
  • 131
  • 2
  • 6
6

use this

import 'package:intl/intl.dart';


getCurrentDate() {
       return DateFormat('yyyy-MM-dd – kk:mm').format(DateTime.now());
}
Sandeep Pareek
  • 1,636
  • 19
  • 21
5

If you just need to print the year from a Timespan you can simply do:

DateTime nowDate = DateTime.now();
int currYear = nowDate.year; 

print(currYear.toString());

4

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.

Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
4

If you want device's current date just use this

 DateTime now = DateTime.now();
 DateTime date = new DateTime(now.year, now.month, now.day);

Or if u want internet time, then use below plugin

ntp: ^2.0.0

 import 'package:ntp/ntp.dart';

 final int offset = await NTP.getNtpOffset(
            localTime: DateTime.now(), lookUpAddress: "time.google.com");
        DateTime internetTime = DateTime.now().add(Duration(milliseconds: offset));
DateTime internetTime = new DateTime(now.year, now.month, now.day);

Or if you need internet time but you don't want to use plugin then use api call

"http://worldtimeapi.org/api/timezone/Asia/Kolkata"
{
  "abbreviation": "IST",
  "client_ip": "136.232.222.86",
  "datetime": "2022-09-30T17:13:10.299478+05:30",
  "day_of_week": 5,
  "day_of_year": 273,
  "dst": false,
  "dst_from": null,
  "dst_offset": 0,
  "dst_until": null,
  "raw_offset": 19800,
  "timezone": "Asia/Kolkata",
  "unixtime": 1664538190,
  "utc_datetime": "2022-09-30T11:43:10.299478+00:00",
  "utc_offset": "+05:30",
  "week_number": 39
}
Shahood ul Hassan
  • 745
  • 2
  • 9
  • 19
Anand
  • 4,355
  • 2
  • 35
  • 45
3

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.

Mayur Dhurpate
  • 1,112
  • 4
  • 16
  • 34
3

In case someone need the simplest way to format date/time in flutter, no plugin needed:

var MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

String formattedDateTime() {
    DateTime now = new DateTime.now();
    return now.day.toString()+" "+MONTHS[now.month-1]+" "+now.year.toString()+" "+now.hour.toString()+":"+now.minute.toString()+":"+now.second.toString();
}

example result: 1 Jan 2020 07:30:45

Change:

  • MONTHS array to show the month in any language
  • the return string as needed: to show date only, time only, or date in different format (dd/mm/yyyy, dd-mm-yyyy, mm/dd/yyyy, etc.)
Niu Bee
  • 53
  • 4
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 06 '22 at 15:05
2

use this:

final now_date = DateFormat('d - M - yyyy ').format(DateTime.now());
1

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)

Daniel Sogbey
  • 163
  • 1
  • 11
0

Only one line with 'intl' package

String todayDate = DateFormat('yMd').format(DateTime.now());
Buddhika
  • 116
  • 2
  • 10
-3

You can use the day in DateTime.now()

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
Gayan Chinthaka
  • 521
  • 6
  • 5