Reputation: 127
I'm using silverlight toolkit TimePicker for allowing the user to pick a particular time. I'm using the following code to convert the time to string,
String Time = Timepicker.Value.Value.TimeOfDay.ToString();
I get the value like "03:24:20", but i just want the value in hh:mm("03:24") format. How can i do that?
Thanks in advance.
Upvotes: 1
Views: 6116
Reputation: 1513
In case of 24 hour format,the following works for me.
dtpStartTime.Value.ToString("HH:mm", CultureInfo.InvariantCulture);
Upvotes: 1
Reputation: 8348
To be dynamic, try this ...
public DateTime timeValue { get; set; }
timeValue = timePicker.Value.Value;
<TextBlock Name="timeText" Text="{Binding StringFormat=hh:mm}"/>
timeText.DataContext = timeValue;
Hope this helps you. And to get a date value alone in TextBlock,
public DateTime dateValue { get; set; }
dateValue = datePicker.Value.Value;
<TextBlock Name="dateText" Text="{Binding StringFormat=d}"/>
dateText.DataContext = dateValue;
Upvotes: 3
Reputation: 806
If you just want hh:mm format , then do the following,
DateTime? _datetime = Timepicker.Value;
String Time = _datetime.Value.Hour + ":" + _datetime.Value.Minute;
Upvotes: 2
Reputation: 63065
try with
String Time = Timepicker.Value.ToString("hh:mm", CultureInfo.InvariantCulture);
if you need 24-hour clock then use HH
instead of hh
You better read the documentation on MSDN : Custom Date and Time Format Strings
Upvotes: 1
Reputation: 8231
If your time is 24hours, try:
Timepicker.Value.Value.TimeOfDay.ToString("HH:mm")
,
else if your time is 12hours, try:
Timepicker.Value.Value.TimeOfDay.ToString("hh:mm")
,
Upvotes: 4