N. Sch
N. Sch

Reputation: 687

How do I get the AM/PM value from a just a Time not a date?

Why does this work,

@string.Format("{0:hh:mm:ss tt}", DateTime.Now)

But not this?

@string.Format("{0:hh:mm:ss tt}", worker.BegginingTime)

worker.BegginingTime is a TimeSpan (edited due to comments) received from the database not a date. How would I format just a TimeSpan correctly with am or pm?

Upvotes: 0

Views: 1128

Answers (2)

Hadee
Hadee

Reputation: 1402

Use

string.Format("{0:t}", (DateTime)worker.BegginingTime))

Upvotes: -2

Jonathan Allen
Jonathan Allen

Reputation: 70307

You'll need to convert it into a DateTime or write a custom formatter.

@string.Format("{0:hh:mm:ss tt}", new DateTime().Add(worker.BegginingTime))

Technically speaking TimeSpan doesn't store time of day, but rather duration.

Upvotes: 3

Related Questions