user3008134
user3008134

Reputation: 127

How to get time from Timepicker in hh:mm format in windows phone 8 app?

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

Answers (5)

Pabitra Dash
Pabitra Dash

Reputation: 1513

In case of 24 hour format,the following works for me.

dtpStartTime.Value.ToString("HH:mm", CultureInfo.InvariantCulture);

Upvotes: 1

Balasubramani M
Balasubramani M

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

Aju
Aju

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

Damith
Damith

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

Chris Shao
Chris Shao

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

Related Questions