简体   繁体   中英

How to convert 3 digits integer to datetime in specific format?

My current problem is when I got 3 digits from database.

Example: int digit = 650. What I need is to convert this 3 digit number into time like 6:30 AM/PM.

I already tried DateTime.ParseExact , but my string wasn't recognized as valid type of DateTime.

This is my code what I already tried.

DateTime dt = DateTime.ParseExact(string.Format("{0}", InternalLoadingListS3Item.U_CAS_NAKL.ToString()), "HHmm", CultureInfo.InvariantCulture);

I just want to convert this 3 digit number into specific time format: 6:30 for example.

If a understand well...

int time = 650;

int hours = time / 100;
int minutes = time % 100;

DateTime date = DateTime.Today.AddHours(hours).AddMinutes(minutes);

I use the date of the current day since it's not clear about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM