Mark
Mark

Reputation: 2760

Add 12 hours to time.now in c#

I am adding 12 hours to the current time. but the current time is displayed in the text box, what is wrong with the code

 DateTime expiresAt = System.DateTime.Now.AddHours(12);
 txt_ExpiresBy.Text = expiresAt.ToString(@"dd/MM/yyyy hh:mm:ss");

Upvotes: 1

Views: 2784

Answers (1)

Paolo Tedesco
Paolo Tedesco

Reputation: 57302

Maybe you are adding 12 hours and you don't see the difference between X AM and X PM?
Try using HH (hour in 24 hours format) instead of hh (hour in 12 hours format) in the format string, or adding the AM/PM indicator tt:

// 24 hours format
expiresAt.ToString(@"dd/MM/yyyy HH:mm:ss");

// 12 hours + am/pm
expiresAt.ToString(@"dd/MM/yyyy HH:mm:ss tt");

See Custom Date and Time Format Strings for a complete reference.

Upvotes: 14

Related Questions