如何将1400-1500转为下午2点 - 3点? [英] How to convert 1400-1500 to 2pm - 3pm?

查看:101
本文介绍了如何将1400-1500转为下午2点 - 3点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


1500-1600

我正在从服务中收到代表服务字段的数据。 >

这意味着下午3点到4点。



我将列出这些例如


1200-1300



1300-1400



1400-1500


我必须在UI中表示为


12pm - 1pm



1pm - 2pm



2pm - 3pm


不幸的是,这个列表可能是随机的。



我的问题是有没有办法使用DateTime对象能够将24小时的时间转换为12小时的时间,还有一种订购顺序的方法?



目前,我感觉我必须编写一个自定义的解析函数,但是想知道有没有人知道如何更好地做到这一点?或者可以建议他们如何实现这一点。

解决方案

如果你有四位数字串(1200,1300)始终为四位数(例如09上午9点是0900),则可以调用静态 ParseExact 方法 DateTime 结构如下所示:

  DateTime dt = DateTime.ParseExact(时间,HHmm,formatInfo); 

formatInfo 是一个 IFormatProvider 界面实现。



如果您有三位数的字符串,则可以使用以下内容:

  DateTime dt = DateTime.ParseExact(time,Hmm,formatInfo); 

但是,我建议使用一个四位数的字符串,因为一个时间像 121。



从那里,如果要输出am / pm等等,你只需调用 ToString 方法,传递以下格式:

  string ampm = dt.ToString(htt); 


I am receiving data representing a time slot from a service as a string in the form of:

1500-1600

This meaning 3pm to 4pm.

I will have a list of these e.g.

1200-1300

1300-1400

1400-1500

and I have to represent this in the UI as

12pm - 1pm

1pm - 2pm

2pm - 3pm

Unfortunately this list could be in a random order.

My question is is there any way of using the DateTime object to be able to convert a 24 hour time to a 12 hour time and also is there a way of ordering times in order?

At the moment I am feeling I will have to write a custom parsing function but wondering if anyone knows how to do this better? or could advise on how they would achieve this.

解决方案

If you have the four digit strings (1200, 1300) and it will always be four digits (e.g. 9 am is 0900) then you can call the static ParseExact method on the DateTime structure like so:

DateTime dt = DateTime.ParseExact(time, "HHmm", formatInfo);

formatInfo is an IFormatProvider interface implementation.

If you have a three digit string, then you can use this:

DateTime dt = DateTime.ParseExact(time, "Hmm", formatInfo);

However, I would recommend using a four-digit string because of the ambiguity presented with a time like "121".

From there, if you want to output am/pm, etc, etc, you would just call the ToString method, passing the following format:

string ampm = dt.ToString("htt");

这篇关于如何将1400-1500转为下午2点 - 3点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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