How to get a certain date in Flutter [duplicate] - flutter

This question already has answers here:
Add/Subtract months/years to date in dart?
(11 answers)
Closed 3 months ago.
I am trying to get the current day then getting the day before it, how can I get that date then convert it into a String?
DateTime now = new DateTime.now();

You can do this by doing:
DateTime.now().subtract(Duration(days:1))
Source: https://api.flutter.dev/flutter/dart-core/DateTime-class.html

You can do this :
DateTime now = DateTime.now();
DateTime nowMinus1Day = now. subtract(const Duration(days: 1));
print(nowMinus1Day.toIso8601String());

you can try this
var now = DateTime.now();
var startDate= now.subtract(Duration(days: 1));
print(startDate);

Related

Convert milliseconds to Firestore date

In Flutter I convert dates to milliseconds like this:
static DateTime createDateTimeNow() {
return DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day, 0, 0, 0, 0, 0);
}
// This create a number value like 1660341600000
final Number millisecondsNow = Helpers.createDateTimeNow().toUtc().millisecondsSinceEpoch;
// I can also format is as String to be presented based on the language
final String dateNow = DateFormat.yMMMMd(language).format(millisecondsNow);
Now I have Firebase Cloud Functions that should read these dates and send notification when they expire, but could I convert this number to a normal date in Firestore?
From Flutter I do something like this:
final DateTime dateNow = DateTime.fromMillisecondsSinceEpoch(1660341600000);
You can use fromMillis( ) method from firestore.timestamp which can create new timestamp from the given number of milliseconds.
firestore.Timestamp.fromMillis(milliseconds)
You can refer these documents for more details firestore timestamp from milliseconds and Firestore Timestamp
You're working too hard. Firestore Timestamp to Dart DateTime:
final dt = aTimestamp.toDate();
and from DateTime to Timestamp:
final ts = Timestamp.fromDate(aDt);
as described in https://pub.dev/documentation/cloud_firestore_platform_interface/latest/cloud_firestore_platform_interface/Timestamp-class.html
and there's no need to think about "milliseconds".

How to get particular month starting date of current year in flutter

how to get current year starting date or particular month starting date programmatically in flutter
You can find first month with the first day (1st January) of the current year easily by doing this
DateTime(DateTime.now().year) //specify only current year
by doing this you will get a date as
2022-01-01 00:00:00.000
import 'package:intl/intl.dart';
main() {
static final DateTime now = DateTime.now();
static final DateFormat formatter = DateFormat('yyyy-MM-dd');
final String formatted = formatter.format(now);
print(formatted); // something like 2022-04-20
}

How to Convert date in string type to another date format. | Flutter | Dart [duplicate]

This question already has answers here:
How do I convert a date/time string to a DateTime object in Dart?
(8 answers)
How do I format a date with Dart?
(24 answers)
Closed 1 year ago.
I have a string date and I want to convert it in another format. For eg : I have a date "25/04/2020" as string and I want to convert this to the string as "25 Apr 2020" like this.
"10/02/2020" become "10 Feb 2020",
"27/11/2021" become "27 Nov 2021", like this.
Use package intl
import 'package:intl/intl.dart';
void main() {
var dateString = "25/04/2020";
var dateTime = DateFormat('dd/MM/yyyy').parse(dateString);
print(DateFormat('dd MMM yyyy').format(dateTime));
}
Try below code hope its help to you.Used intl package here
DateTime now = DateTime.now();
String formattedDate = DateFormat('dd MMM yyyy ').format(now);
print(formattedDate);
Your Widget:
Text(
formattedDate,
),
Your result screen->

Need current week day name with date which can be used further [duplicate]

This question already has answers here:
How get the name of the days of the week in Dart
(6 answers)
Closed 2 years ago.
I need This current weeks day name, with the date like (01/12/2020). I can select the data, so that I can use the data further.
/////////////////////////////////////////////////////// BUILD YOUR OWN
///////////////////////////////////////////////
Add this to your package's pubspec.yaml file:
dependencies:
intl: ^0.16.1
Import the package in the dart file
import 'package:intl/intl.dart';
...
var dateString = '01/12/2020';
var date = DateFormat('d/M/yyyy').parse(dateString);
List<String> daysInWeek=['Monday', 'Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday',];
print(daysInWeek[date.weekday-1]);
I think what you're wanting is the actual name of the day of the week. In that case, this should do the trick:
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var day = DateTime.parse('2020-09-12').weekday;
print(days[day]); // prints "Saturday"
Try pasting this code in dartpad to see how it works

Convert simple date into SQLite datetime format?

I have date in new Date() format. I need to convert to SQLite date time format.(2014-01-05T00:00:00.000Z). Is it possible to do so??
var date = new Date();
now I need to convert this date to SQLite datetime format like (2014-01-05T00:00:00.000Z).
Try:
var date = new Date();
var sqllite_date = date.toISOString();
Use this
// Create an instance of the Date class
var date = new Date();
// Convert it to an ISO string
var sqliteDate = Date.toISOString();
take a look at this W3schools
You can use strftime, from date and time functions.
SELECT strftime('%d-%m-%Y', 'now')
Output
10-06-2010
Hope this helps ..:)