I´m trying to create a datetime select in flutter. I have the frontend but I need to create function to show date time selector. I follow this video but i don´t know why return
Undefined Class 'Datetime'`
DateTime date = new DateTime.now();
Future<Null> selectDate(BuildContext context) async {
final Datetime picked = await showDatePicker(
context: context,
initialDate: date,
firstDate: new DateTime(2010),
lastDate: null);
if (picked != null && picked != date) {
print('date selected: ${date.toString()}');
setState(() {
date = picked;
});
}
}
So anybody has idea what is wrong?
You have a typo declaring your picked variable - should be DateTime with a capital T.
Related
I am using a DatePicker in Xamarin, but am confused how to store the selected date to a variable whenever a new date is selected in the DatePicker.
This is my current code:
private void DateSelected(object sender, DateTime e)
{
DateTime date = (DateTime)e;
Debug.WriteLine(date);
}
But the date keeps defaulting to 1/1/0001
Your event handler isn't quite right. e is not a DateTime but a DateChangedEventArgs, which contains the DateTime selected. Your code should be:
private void DatePicker_DateSelected(object sender, DateChangedEventArgs e)
{
DateTime newDate = e.NewDate;
}
I have a requirement of converting Stream<List<T>> to Stream<Map<K,T>>
I have a class
class Order
{
int id;
DateTime date;
}
I want to convert Stream<List<Order>> to Stream<Map<DateTime, List<Order>>
I want to display orders as below
12-Dec-2020
Order 1
Order 2
13-Dec-2020
Order 3
Order 4
14-Dec-2020
Order 5
Order 6
Suggestions for a better DS are welcome.
How do I do this?
Please suggest.
Thanks in advance.
Using groupListsBy from collection: ^1.15.0
K keySelector(T t) { ... }
final Stream<List<T>> source$ = ...;
final Stream<Map<K, List<T>>> result$ = source$.map((list) => list.groupListsBy(keySelector));
You can do this without any package.
First, you have to create an empty Map, then map a List<Order> to a map with a key as a date from the list item (single order) then add it to That Empty map.
Check if the date is available as a key in Map. If the date exists as a key in that map then simply add a new order to that value or else simply add a new key to the Map.
Code:
Stream<List<Order>> orders = ...;
Stream<Map<DateTime, List<Order>>> ordersByDate = {};
orders.forEach((Order order){
ordersByDate.keys.contains(order.date)
? ordersByDate.update(order.date, (oldValue) => oldValue + <Order>[order])
: ordersByDate[order.date] = [order];
});
print(ordersByDate);
//At here your ordersByDate
Let me know if it works for you.
Fold method is what you're looking for.
Example:
void main() async {
final today = DateTime.now();
final tomorrow = today.add(const Duration(days: 1));
final order1 = Order(1, today);
final order2 = Order(2, today);
final order3 = Order(3, tomorrow);
final x = await Stream
.fromIterable([order1, order2, order3])
.fold<Map<DateTime, List<Order>>>({}, (val, element) {
(val[element.date] ??= []).add(element);
// Same as:
// if (val[element.date] == null) {
// val[element.date] = [];
// }
// val[element.date]!.add(element);
return val;
});
print(x);
}
class Order
{
int id;
DateTime date;
Order(this.id, this.date);
#override
String toString() {
return 'Order(id: $id, date: $date)';
}
}
how are you ?
my backend just accept date and time in this format 2019-03-24 11:00:00
and i use in my app calendar widget to get the date and it print the date in this format 2019-04-24 12:00:00.000Z and i use this code to get the time
TimeOfDay _time = new TimeOfDay.now();
Future<Null> _selecTime(BuildContext context) async {
final TimeOfDay picked = await showTimePicker(
context: context,
initialTime: _time,
);
if (picked != null && picked != _time) {
print('${_time.toString()}');
setState(() {
_time = picked;
});
}
}
and i get this TimeOfDay(07:37)
so how can i get format for date like this 2019-04-24 and for time like this 11:00:00 and i will deal with it
thanks
You can use intl package
Here is an example:
import 'package:intl/intl.dart';
main() {
var now = new DateTime.now();
var dateFormat = new DateFormat('yyyy-MM-dd HH:mm:ss');
String formattedDate = dateFormat.format(now);
print(formattedDate); // 2019-04-28 09:02:29
}
You can do this task without any third-party package only by extensions.
void main() {
DateTime now = DateTime.now();
print(now.getTime24());
}
extension ExtractTime on DateTime {
String getTime24() => "$year-${month.addZero()}-${day.addZero()} ${hour.addZero()}:${minute.addZero()}:${second.addZero()}";
}
extension AddZero on int {
String addZero() => "${"$this".length==1?'0$this':this}";
}
Please see content below, confused on what to put for return that has type Date
#Override
public int maxCapacity() {
return 3;
}
#Override
public String area() {
return "10";
}
#Override
public Date whenBuilt() {
return ***[What should I return here?]***
}
Date methods must to return Date instance or null..
You can just return new Date();, but it will always fresh date.
Method whenBuilt looks like some date in the past, so you should to make some date, in constructor for example.
class A {
private final Date createdAt;
A() {
createdAt = new Date();
}
public Date whenBuilt() {
return createdAt;
}
}
#Override annotation points to your class is extension from some base class.
Try to check base class, may be built date is already in there.
Default calendar widget of Smartgwt Component shows date as Sun 10/31 ( MM/DD format ) .
http://www.smartclient.com/smartgwt/showcase/#simple_calendar_category
How can I change the date format to DD/MM formatin Calendar Widget ?
Thanks in advance for having a look,
Sachin
SmartGwt provides a static DateUtil for changing date formats.Put the following code somewhere in your application and it will change the date format through out the application
DateUtil.setShortDateDisplayFormatter(new DateDisplayFormatter() {
public String format(Date date) {
if(date == null) return null;
final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("DD/MM/yyyy");
String format = dateFormatter.format(date);
return format;
}
});
Take a look to the com.smartgwt.client.util.DateUtil specifically to the methods setShortDateDisplayFormat and setShortDateDisplayFormatter
This should solve your problem, according to the documentation.
put
this.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
in constructor
#aruns is correct, however another step is need if you are using RelativeDateItem
DateUtil.setShortDatetimeDisplayFormatter(new DateDisplayFormatter()
{
public String format(Date date)
{
if(date == null)
{
return null;
}
else
{
final DateTimeFormat dateFormatter = DateTimeFormat.getFormat(DATE_FORMAT);
return dateFormatter.format(date);
}
}
});
DateUtil.setDateParser(new DateParser()
{
public Date parse(String dateString)
{
final DateTimeFormat format = DateTimeFormat.getFormat(DATE_FORMAT);
return format.parse(dateString);
}
});
The DateFormat is defined in the GWT documentation