字符串表示日期,并重新格式化 [英] string represents date, and reformat it

查看:134
本文介绍了字符串表示日期,并重新格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下情况:
有一个字符串具有此格式2012-02-25 07:53:04



但最后,我宁愿最终得到这个格式25-02-2012 07:53:04



我想我有2个选项。 1将重新格式化字符串并将其全部移动,但我不认为这是一个干净的方法。



我正在考虑的另一种方法是将源字符串保存到date参数,然后将date参数写回到特定日期格式的字符串。
但这是否还可以做?

解决方案

执行此操作:

  DateTime.Parse(2012-02-25 07:53:04)ToString(dd-MM-yyyy hh:mm:ss); 

请记住,这不是文化意识。如果你需要存储中间的结果,那么你可以这么简单:

  var myDate = DateTime.Parse 2012-02-25 07:53:04); 
var myDateFormatted = myDate.ToString(dd-MM-yyyy hh:mm:ss);

最后,请查看 TryParse() ,如果您不能保证输入格式始终有效。 p>

The following case: There is a string that has this format "2012-02-25 07:53:04"

But in the end, i rather want to end up with this format "25-02-2012 07:53:04"

I think i have 2 options. 1 would be to reformat the string and move it all around, but i dont think this is a clean way of doing this.

A other way that i was thinking about is to save the source string to a date parameter, and then write the date parameter back to a string in a certain date format. But is this even possible to do ?

解决方案

Do this:

DateTime.Parse("2012-02-25 07:53:04").ToString("dd-MM-yyyy hh:mm:ss");

Keep in mind this isn't culture-aware. And if you do need to store the intermediate result you could do that just as easily:

var myDate = DateTime.Parse("2012-02-25 07:53:04");
var myDateFormatted = myDate.ToString("dd-MM-yyyy hh:mm:ss");

Lastly, check out TryParse() if you can't guarantee the input format will always be valid.

这篇关于字符串表示日期,并重新格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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