Ghazanfar Khan
Ghazanfar Khan

Reputation: 3718

Get date from DatePicker using c# in windows store app

I want to insert date value in my database I have a class which have a date time attribute like this

public DateTime BookingDate { get; set; }

I have a date picker in my xaml page named datepicker . I want to insert only date value , How to get date using c#??

Upvotes: 0

Views: 3166

Answers (2)

Zach Olivare
Zach Olivare

Reputation: 4161

Using an MVVM Pattern:

View (XAML):

<DatePicker Date="{Binding DateOffset, Mode=TwoWay}" />

ViewModel (C#):

private DateTime Date { get; set; }
public DateTimeOffset DateOffset
{
    get { return DateTime.SpecifyKind(Date, DateTimeKind.Utc); }
    set { Date = value.DateTime; }
}

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222712

You can use the Date Property , follow the documentation

  DateTimeOffset sourceTime = YourDatePicker.Date;
  BookingDate = sourceTime.DateTime;

To convert back to offset and bind to datetimepicker

   DateTime newBookingDate;
   newBookingDate = DateTime.SpecifyKind(BookingDate, DateTimeKind.Utc);
   DateTimeOffset bindTime = newBookingDate;
   YourDatePicker.Date = bindTime;

Adding a DatePicker (XAML)

Upvotes: 2

Related Questions