如何在C#中更改日期格式 [英] How to change date format in C#

查看:110
本文介绍了如何在C#中更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我已经在本地服务器上托管了一个应用程序,因为我在本地系统中使用date.my日期格式是dd / MM / yyyy,但我的服务器日期格式是dd-MM-yyyy。所以当我试图解析日期时间时,它会给我错误。



我尝试了什么:



Hello i have hosted a application on local server in that i am taking the date.my date format in local system was dd/MM/yyyy but my server date-format was dd-MM-yyyy. so when i am trying to parse the date time it is giving me error.

What I have tried:

DateTime.ParseExact(dpSelectDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);





以上代码我正在解析datepicker但它无效。



with above code i am parse the datepicker but it is not working.

推荐答案

使用TryParseExact并且如果一种格式无法转换尝试另一个。您还可以使用日期选择器控件,以便日期始终采用设置格式,无论机器处于何种状态。
Use TryParseExact and if one format fails to convert try another. You could also look to use a date picker control so that the date is always in a set format regardless the machine it is on.


这篇关于如何在C#中更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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