I'm creating an app with Xamarin Forms and I want to implement a datetimepicker like this in iOS
https://developer.apple.com/documentation/uikit/uidatepicker
I have found how to implement a TimePicker or a DatePicker but not all in one. Is there any way to add it to Xamarin Forms?
Is there any way to create it in Xamarin Forms?
<DatePicker
x:Name="inicioDatePicker"
Format="D"
DateSelected="OnDateSelected"
Date="{Binding InicioDatePicker}"/>
Thank you. Regards
You should add Event when the value of Picker changed.Refer the following code
//...
using ObjCRuntime;
//...
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if (Control != null)
{
UIDatePicker dateTimePicker = (UIDatePicker)Control.InputView;
dateTimePicker.Mode = UIDatePickerMode.DateAndTime;
dateTimePicker.AddTarget(this, new Selector("DateChanged:"), UIControlEvent.ValueChanged);
NSDateFormatter dateFormat = new NSDateFormatter();
dateFormat.DateFormat = "dd/MM/yyyy HH:mm";
var text = (UITextField)Control;
text.Text = dateFormat.ToString(dateTimePicker.Date);
}
}
[Export("DateChanged:")]
public void DateChanged(UIDatePicker picker)
{
NSDateFormatter dateFormat = new NSDateFormatter();
dateFormat.DateFormat = "dd/MM/yyyy HH:mm";
var text = (UITextField)Control;
text.Text = dateFormat.ToString(picker.Date);
}
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 want to use Date and Time picker at the same time. But i can't get value of Time from Xamarin DatePicker just get Date value correctly.
I shared codes below
CustomDatePicker.cs
public class CustomDatePickerRenderer : DatePickerRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if(Control != null)
{
UITextField entry = Control;
UIDatePicker picker = (UIDatePicker)entry.InputView;
picker.PreferredDatePickerStyle = UIDatePickerStyle.Wheels;
picker.MinimumDate = Foundation.NSDate.Now;
picker.MaximumDate = Foundation.NSDate.Now.AddSeconds(172799);
picker.Mode = UIDatePickerMode.DateAndTime;
}
}
}
Xaml
<DatePicker x:Name="datePickerIOS" DateSelected="datePickerIOS_DateSelected" ></DatePicker>
Xaml.cs
private void datePickerIOS_DateSelected(System.Object sender, Xamarin.Forms.DateChangedEventArgs e)
{
var date = datePickerIOS.Date;
// This date's time value always return 12:00:00 AM
}
As you use DateSelected="datePickerIOS_DateSelected" method,it was the event of the datepicker,you could only get the correct date.
You should get the correct date and time in your customrenderer and return it to your page(you could use MessagingCenter to achieve this).
You could refer to this link.
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}";
}
I have "2011-12-05" and I want to convert this to "Monday 05-Dec-2011".
My date conversion code depends on the device timezone. If my timezone is India, then I get date Monday 05-Dec-2011 and if my timezone is Kingston, Jamaica, I get Sunday 04-Dec-2011.
For this reason my application does not display the correct date for different timezones.
Is there any solution to convert date without Blackberry Date class or using current Date and Time Zone?
I want to only convert this date to String
I am converting this date using below function
public static String reformatMonthDate(String source)
{
SimpleDateFormat write = new SimpleDateFormat("dd MMM yyyy"); //YYYY-MMM-dd
Date date = new Date(HttpDateParser.parse(source));
return write.format(date);
}
You can specify a specific locale, instead of relying on the system's default.
Locale locale = Locale.get(Locale.LOCALE_fr);
// Parse with HttpDateParser
Date date = new Date(HttpDateParser.parse("2002-01-29"));
// Format with a custom format and locale
DateFormat formatter = new SimpleDateFormat("E, dd MMM yyyy", locale);
StringBuffer buf = new StringBuffer(30);
String s = formatter.format(date, buf, null).toString(); // mar., 29 janv. 2002
DateTimePicker gives the current date according to the location or device configuration settings.
Try this code:
You should get your requirement.
public class LoadingScreen extends MainScreen implements FieldChangeListener
{
private String select_Date[]={"Select Date"};
private String month_ar[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
private ObjectChoiceField choiceField;
private ButtonField show;
public LoadingScreen()
{
createGUI();
}
private void createGUI()
{
choiceField=new ObjectChoiceField("Select Date: ", select_Date, 0)
{
protected boolean navigationClick(int status, int time)
{
DateTimePicker datePicker = DateTimePicker.createInstance( Calendar.getInstance(), "dd/MM/yyyy",null);
datePicker.doModal();
Calendar cal1=datePicker.getDateTime();
String day="";
String mon="";
if(String.valueOf(cal1.get(Calendar.DAY_OF_MONTH)).length()==1)
{
day="0"+String.valueOf(cal1.get(Calendar.DAY_OF_MONTH));
}
else
{
day=String.valueOf(cal1.get(Calendar.DAY_OF_MONTH));
}
mon=String.valueOf(cal1.get(Calendar.MONTH));
String month=""+month_ar[cal1.get(Calendar.MONTH)];
select_Date[0]=day+"-"+month+"-"+cal1.get(Calendar.YEAR);
choiceField.setChoices(select_Date);
return true;
}
};
add(choiceField);
show=new ButtonField("Show",FIELD_HCENTER);
show.setChangeListener(this);
add(show);
}
public void fieldChanged(Field field, int context)
{
if(field==show)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert(select_Date[choiceField.getSelectedIndex()]);
}
});
}
}
public boolean onMenu(int instance)
{
return true;
}
protected boolean onSavePrompt()
{
return true;
}
}
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