如何转换时间跨度至下午或凌晨时间? [英] How to convert timespan to pm or am time?

查看:205
本文介绍了如何转换时间跨度至下午或凌晨时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我存储用户时间在UTC时间,当我告诉它,我需要将其转换为上午下午的时间。

下面是示例数据库中,我有17:00:00转换至下午5:00

下面是code我想出了这么远,但它不工作

  VAR时间= DateTime.ParseExact(object.Time.ToString(),HHMM,CultureInfo.CurrentCulture)的ToString(HH:MM TT);


解决方案

  VAR时间= DateTime.ParseExact(17:00,HH:MM,NULL)的ToString( HH:MM TT);

返回05:00 PM

DateTime.ParseExact将返回日期时间。

编辑:

包含的CultureInfo

  VAR时间= DateTime.ParseExact(17:00,HH:MM,NULL)的ToString(HH:MM TT,CultureInfo.GetCultureInfo(恩我们));

I'm storing user time in UTC time and when I show it I need to convert it to am pm time.

Here is example in database I have 17:00:00 convert to 5:00 pm

Here is the code what I came up so far but it's not working

var time = DateTime.ParseExact(object.Time.ToString(), "HHmm", CultureInfo.CurrentCulture).ToString("hh:mm tt");

解决方案

var time = DateTime.ParseExact("17:00", "HH:mm", null).ToString("hh:mm tt");

returns 05:00 PM

DateTime.ParseExact is returning DateTime

Edited:

Include CultureInfo

var time = DateTime.ParseExact("17:00", "HH:mm", null).ToString("hh:mm tt", CultureInfo.GetCultureInfo("en-US"));

这篇关于如何转换时间跨度至下午或凌晨时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆