How to display month using ```DateTime``` in flutter - datetime

I have a calendar where i want to display the month above the calendar. Is there anyway to display only the current month?
(credits to https://github.com/adamstyrc/ for the image)
it should look like this :

Use the below package for DateFormat.
https://pub.dev/packages/intl
String returnMonth(DateTime date) {
return new DateFormat.MMMM().format(date);
}
Just pass the date, the function will return Month. For ex. April,January

Get intl dependency from here pubdev-intl.
Import the dependency by adding it to pubspec.yaml.
import 'package:intl/intl.dart';
List months =
['jan', 'feb', 'mar', 'apr', 'may','jun','jul','aug','sep','oct','nov','dec'];
var now = new DateTime.now();
current_mon = now.month;
print(months[current_mon-1]);

It´s very easy with date_formatter;
Install date_format: ^2.0.2 in pubspec. link
use this code:
String returnThisMonth(){
var month = DateTime.now();
final formatted = formatDate(month, [mm]);
print(formatted);
return formatted;
}
It will return you a string like 01, 02, 03, as january, february or march. Then you can use it freely.

You can try table_calendar from the pub which is fully customizable and also you can able get current date current month selected date
table_calender

You can use the DateFormat class:
String getMonth(int currentMonthIndex) {
return DateFormat('MMM').format(DateTime(0, currentMonthIndex)).toString();
}
//currentMonthIndex 1 => "Jan", 2 => "Feb", ...
The docs give a list of all skeletons (M, MMM, m, MMMM) and named constructors that can be used. ex. (MONTH, NUM_MONTH, ABBR_MONTH)
So alternatively:
String languageCode = Localizations.localeOf(context).languageCode;
DateFormat dateformat1 = DateFormat.MMMM(languageCode);
DateFormat dateformat2 = DateFormat.MMM(languageCode);
String this_month = dateformat1.format(DateTime.now());
//ex => August
String next_month = dateformat2.format(DateTime.now().add(Duration(days: 30)));
//ex => Sep

Related

Need to convert my Time string to a timestamp to update current timestamp field in firestore

I am trying to convert my Time string that displays in my app as 11:00. I need to convert to a timestamp so I can replace the current Time field in my firestore which is a timestamp.
I have tried using moment.js to update the field but it changes the data type to a string.
Current value in my firestore is shown below
submitJob = () => {
const { navigation } = this.props;
const customer_jobnumber = navigation.getParam('JobNumber', 'No Job Number');
const customer_starttime = navigation.getParam('datetime', 'No Job Start Time');
const customer_end = navigation.getParam('datetimefinish', 'No Job Finish Time');
firebase.firestore().collection("jobs").doc(customer_jobnumber).update({ endtime: customer_end, starttime: customer_starttime, value: firebase.firestore.FieldValue.serverTimestamp() });}
The desired value would be - October 4, 2020 at 11:00:00 AM UTC+1
Found the solution with this, if anyone comes across the same problem.
let tx = customer_starttime.split(":")
let dx = new Date().setHours(parseInt(tx[0]),parseInt(tx[1]),0)
let dl = new Date(dx)

SwiftUI - How to format Firestore Timestamp in a TextView

I have a startTime field returned from Firestore which is defined as a timestamp. I am using Codable to specify startTime as a date in Swift.
I cannot see how to convert the full date provided by timestamp to just a time.
Text("\(startTime)" gives me "Monday, September 7, 2020 at 12:00:00 PM British Summer Time"
Use the following example as reference to get the format you want
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
let newDateString = dateFormatter.string(from: yourDate)
Here you can find the different format options.
Here is a more complete (ready to use) answer:
import Foundation
import FirebaseFirestore
func formatTransactionTimpestamp(_ timestamp: Timestamp?) -> String {
if let timestamp = timestamp {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
let date = timestamp.dateValue()
dateFormatter.locale = Locale.current
let formatted = dateFormatter.string(from: date)
return formatted
}
return ""
}
Apple docs
Firebase docs

DateTime in UTC not converting to Local

I'm receiving a DateTime response from API that's sets the timezone to UTC.
But when I try to convert the received data using toLocal() it doesn't convert.
my local time is HKT
here's my code.
//TIME DIFFERENCE
getNotificationDate(DateTime date) {
date = date.toUtc();
final convertedDate = date.toLocal();
final dateNow = DateTime.now();
print('TIMENOW: ' + dateNow.toString());
print('TIMENOTIFC: ' + convertedDate.toString());
final difference = dateNow.difference(convertedDate);
print('DIFFERENCE: ' + difference.toString());
return getDurationFormat(difference);
}
EDIT:
date is the DateTime I'm receiving from the API. which is in UTC timezone.
I used print('TIMEZONENAME: ' + date.timeZoneName; and it automatically sets the timezone to HKT. that's why it does nothing when I try to use date.toLocal()
Flutter gave us the easiest way to convert it.
You just need to pass utc: true while parsing your date.
var dateTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(dateUtc, true);
var dateLocal = dateTime.toLocal();
Input:
Assume my TimeZone : +05:30
UTC Date -> 2020-02-12 23:57:02.000
Output:
Local Date -> 2020-02-12 18:27:02.019660
// you have time in utc
var dateUtc = DateTime.now().toUtc();
print("dateUtc: $dateUtc"); // 2019-10-10 12:05:01
// convert it to local
var dateLocal = dateUtc.toLocal();
print("local: $dateLocal"); // 2019-10-10 14:05:01
Can you see the difference in hours, in utc it is 12 and locally it is 14.
Firstly, convert your Sting to DateTime.
> DateTime dateTime = DateTime.parse(json['pickUpTime']);
Secondly, add timeZoneOffSet to your converted date time it will convert utc to your local time.
> dateTime = dateTime.add(DateTime.parse(json['pickUpTime']).timeZoneOffset);
Final Code
DateTime dateTime = DateTime.parse(json['pickUpTime']);
dateTime = dateTime.add(DateTime.parse(json['pickUpTime']).timeZoneOffset);
You can try this code:
getNotificationDate(DateTime date) {
date = DateTime.utc(date.year,date.month,date.day,date.hour,date.minute,date.second);;
final convertedDate = date.toLocal();
final dateNow = DateTime.now();
print('TIMENOW: ' + dateNow.toString());
print('TIMENOTIFC: ' + convertedDate.toString());
final difference = dateNow.difference(convertedDate);
print('DIFFERENCE: ' + difference.toString());
return getDurationFormat(difference);
}
I've done something like this.
String dateTimeFormatter(String dateTime, {String? format}) {
return DateFormat(format ?? 'yyyy/MM/dd, hh:mm a')
.format(DateTime.parse(dateTime).toLocal())
.toString();
}
just pass the format which you want to display in your app.
If somebody needs to parse a UTC timestamp in isoformat, for example something like this:
>>> from datetime import datetime
>>> datetime.utcnow().format()
'2021-07-20T19:35:19.769891'
Then you can parse this and convert this to local time by
DateTime parseDatetimeFromUtc({required String isoFormattedString}){
var dateTime = DateTime.parse(isoFormattedString + '+00:00');
return dateTime.toLocal();
}
The '+00:00' is append here as the timezone information part which I do not send over my API to save some bytes. Maybe this helps someone who is in the same situation.
Of course you do not need this hardcoded suffix if you use a timezone aware timestamp in your backend:
>>> from datetime import datetime, timezone
>>> datetime.now(timezone.utc).isoformat()
'2021-07-20T19:42:36.538195+00:00'
For those who parsing TimeStamp from Firestore.
*sentAt is Timestamp
String timeToDate = widget.sentAt.toDate().toString();
var dateTime = DateFormat("yyyy-MM-dd HH:mm:ss").parse(timeToDate, true);
var dateLocal = dateTime.toLocal();
this is how i converted to my required time .which was showing as
I/flutter ( 5709): 16 Apr 08:30 PM 2021
when using the
var date=DateFormat("dd MMM hh:mm a y").format(DateTime.fromMillisecondsSinceEpoch(start*1000));
print(date);
but after using this code i got my right time
var date=DateFormat("dd MMM hh:mm a y").format(DateTime.fromMillisecondsSinceEpoch(start*1000).toUtc());
print(date);
which is
I/flutter ( 5709): 16 Apr 03:00 PM 2021
I tried several examples and forums, but it kept getting me the incorrect date time for my zone, The only way I made it work correctly was by using the GMT package
var nowLocal = DateTime.now();
print("toUtc: ${nowLocal.toUtc()}");
print("toLocalDateTime ${nowLocal.toLocalDateTime()}");
print("toLocal ${nowLocal.toLocal()}");
print("toIso8601String ${nowLocal.toIso8601String()}");
final timeZoneOffsetInHours = DateTime.now().timeZoneOffset.inHours;
final nowGMT = await GMT.now();
print("GMT: $nowGMT");
final nowActual = nowGMT?.add(Duration(hours: timeZoneOffsetInHours));
print("nowActual $nowActual");
Two solutions I implemented
var date = DateFormat("yyyy-MM-ddTHH:mm:ss").parse(json, true);
var dateLocal = date.toLocal();
Other solutions add "Z"
You need to indicate a timezone to DateTime.parse, otherwise it assumes local time. From the dartdoc:
var date = DateTime.parse("${dateString}Z").toLocal();
var dateFormat = date2.toLocal();
Install intl package from pub.dev
add following line :
import 'package:intl/intl.dart';
You can make an extension like below so it will be very helpful and easy to use anywhere in a whole project.
//just make sure you have passed the right date format of utc("yyyy-MM-dd HH:mm:ss"). I have passed by default my format.
//for example
// 2020-11-25 24:12:36 -> "yyyy-MM-dd HH:mm:ss"
DateTime localDate=utcDateTime.toLocalDateTime();
//for different formats you can pass your own dateFormat for utcDate like below:
// 20-11-25 24:12:36 -> "yy-MM-dd HH:mm:ss"
DateTime localDate=utcDateTime.toLocalDateTime("yy-MM-dd HH:mm:ss");
extension DateTimeExtension on DateTime {
DateTime toLocalDateTime({String format = "yyyy-MM-dd HH:mm:ss"}) {
var dateTime = DateFormat(format).parse(this.toString(), true);
return dateTime.toLocal();
}
}
convert utc number to DateTime:
DateTime utcToDateTime(int utc) {
return DateTime(1970, 1, 1).add(Duration(seconds: utc));
}
//test
DateTime d = utcToDateTime(1649297709);
print(d);

How do I convert a date/time string to a DateTime object in Dart?

Say I have a string
"1974-03-20 00:00:00.000"
It is created using DateTime.now(),
how do I convert the string back to a DateTime object?
DateTime has a parse method
var parsedDate = DateTime.parse('1974-03-20 00:00:00.000');
https://api.dartlang.org/stable/dart-core/DateTime/parse.html
There seem to be a lot of questions about parsing timestamp strings into DateTime. I will try to give a more general answer so that future questions can be directed here.
Your timestamp is in an ISO format. Examples: 1999-04-23, 1999-04-23 13:45:56Z, 19990423T134556.789. In this case, you can use DateTime.parse or DateTime.tryParse. (See the DateTime.parse documentation for the precise set of allowed inputs.)
Your timestamp is in a standard HTTP format. Examples: Fri, 23 Apr 1999 13:45:56 GMT, Friday, 23-Apr-99 13:45:56 GMT, Fri Apr 23 13:45:56 1999. In this case, you can use dart:io's HttpDate.parse function.
Your timestamp is in some local format. Examples: 23/4/1999, 4/23/99, April 23, 1999. You can use package:intl's DateFormat class and provide a pattern specifying how to parse the string:
import 'package:intl/intl.dart';
...
var dmyString = '23/4/1999';
var dateTime1 = DateFormat('d/M/y').parse(dmyString);
var mdyString = '04/23/99';
var dateTime2 = DateFormat('MM/dd/yy').parse(mdyString);
var mdyFullString = 'April 23, 1999';
var dateTime3 = DateFormat('MMMM d, y', 'en_US').parse(mdyFullString));
See the DateFormat documentation for more information about the pattern syntax.
DateFormat limitations:
DateFormat cannot parse dates that lack explicit field separators. For such cases, you can resort to using regular expressions (see below).
Prior to version 0.17.0 of package:intl, yy did not follow the -80/+20 rule that the documentation describes for inferring the century, so if you use a 2-digit year, you might need to adjust the century afterward.
As of writing, DateFormat does not support time zones. If you need to deal with time zones, you will need to handle them separately.
Last resort: If your timestamps are in a fixed, known, numeric format, you always can use regular expressions to parse them manually:
var dmyString = '23/4/1999';
var re = RegExp(
r'^'
r'(?<day>[0-9]{1,2})'
r'/'
r'(?<month>[0-9]{1,2})'
r'/'
r'(?<year>[0-9]{4,})'
r'$',
);
var match = re.firstMatch(dmyString);
if (match == null) {
throw FormatException('Unrecognized date format');
}
var dateTime4 = DateTime(
int.parse(match.namedGroup('year')!),
int.parse(match.namedGroup('month')!),
int.parse(match.namedGroup('day')!),
);
See https://stackoverflow.com/a/63402975/ for another example.
(I mention using regular expressions for completeness. There are many more points for failure with this approach, so I do not recommend it unless there's no other choice. DateFormat usually should be sufficient.)
import 'package:intl/intl.dart';
DateTime brazilianDate = new DateFormat("dd/MM/yyyy").parse("11/11/2011");
you can just use : DateTime.parse("your date string");
for any extra formating, you can use "Intl" package.
void main() {
var dateValid = "30/08/2020";
print(convertDateTimePtBR(dateValid));
}
DateTime convertDateTimePtBR(String validade)
{
DateTime parsedDate = DateTime.parse('0001-11-30 00:00:00.000');
List<String> validadeSplit = validade.split('/');
if(validadeSplit.length > 1)
{
String day = validadeSplit[0].toString();
String month = validadeSplit[1].toString();
String year = validadeSplit[2].toString();
parsedDate = DateTime.parse('$year-$month-$day 00:00:00.000');
}
return parsedDate;
}
a string can be parsed to DateTime object using Dart default function DateTime.parse("string");
final parsedDate = DateTime.parse("1974-03-20 00:00:00.000");
Example on Dart Pad
String dateFormatter(date) {
date = date.split('-');
DateFormat dateFormat = DateFormat("yMMMd");
String format = dateFormat.format(DateTime(int.parse(date[0]), int.parse(date[1]), int.parse(date[2])));
return format;
}
I solved this by creating, on the C# server side, this attribute:
using Newtonsoft.Json.Converters;
public class DartDateTimeConverter : IsoDateTimeConverter
{
public DartDateTimeConverter()
{
DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.FFFFFFK";
}
}
and I use it like this:
[JsonConverter(converterType: typeof(DartDateTimeConverter))]
public DateTimeOffset CreatedOn { get; set; }
Internally, the precision is stored, but the Dart app consuming it gets an ISO8601 format with the right precision.
HTH

How to convert string in format DD/MM/YYYY to date object in Flex

I am taking date input from user in text field, which is in format DD/MM/YYYY.
How to convert this string to date object in Flex.
Platform: Adobe Flash Builder 4.6
Since Flex SDK 4.10.0 you can use
DateFormatter.parseDateString(s, "DD/MM/YYYY");
Former versions of parseDateString didn't respect a format string, so it cannot parse dateString value formatted with non default en_US format
Use DateField's stringToDate method. DateFormatter also has a parseDateString function but for some reason it's set to protected.
public function convertStringToDate(s:String):Date
{
return DateField.stringToDate(s, "DD/MM/YYYY");
}
If you are not on the latest Apache SDK (I know we aren't because of third party components) you basically have to write your own conversion.
The built in DateFormatter has the static method, parseDateString, but you have no way of specifying the format of the string. It was a bit rubbish!
If you definitely have no localisation issues and are sure the date is ALWAYS in DD/MM/YYYY format you could use the following:
public function stringToDate(date:String):Date {
// Extract year, month and day from passed in date string
var year:int = IntFromSubString(date, 6, 4);
var month:int = IntFromSubString(date, 3, 2);
var day:int = IntFromSubString(date, 0, 2);
// Always remember Flex months start from 0 (Jan=0, Feb=1 etc.) so take 1 off the parsed month
return new Date(year, month-1, day);
}
private static function IntFromSubString(date:String, start:int, length:int):int {
return parseInt(date.substr(start, length)) as int;
}

Resources