将dd/mm/yy转换为dd-mm-yyyy [英] Convert dd/mm/yy to dd-mm-yyyy

查看:83
本文介绍了将dd/mm/yy转换为dd-mm-yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我在array []
中有一个值="9/1/2001" 我需要将其转换为"09-01-2001"

请帮助

Hello,
i have a a value = "9/1/2001" in a array[]
i need to convert it to "09-01-2001"

please help

推荐答案


试试这个将您的字符串转换成mmddyyyy格式
Hi,
try this to convert your string into mmddyyyy format
DateTime.ParseExact(yourDateString, "ddMMyyyy", CultureInfo.InvariantCulture);



祝你好运
快乐编码



Best Luck
Happy Coding


您需要做两件事.首先,将您的字符串转换为DateTime:
There are two things you''ll have to do. First off all convert your string to a DateTime:
string dateString = "9/1/2001";
DateTime dateValue;
if(DateTime.TryParse(dateString, out dateValue)){
   // Convert is ok
}
else{
   // Could not convert
   // The string contains a invalid date or invalid dateformat
}



然后,如果转换正常,则可以按照需要的方式对其进行格式化



Then if the convert is ok, you can format it the way you need

string formatedDate = dateValue.ToString("dd-MM-yyyy");



日期格式模式列表: http://www.geekzilla.co.uk/View00FF7904- B510-468C-A2C8-F859AA20581F.htm [ ^ ]



List of date format patterns: http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm[^]


您有很多答案,但是根本问题不同.它尝试使用数据的字符串表示形式,而不是数据本身.这是错误的.处理数据,在这种情况下为System.DateTime数据.您通常不需要将一个字符串转换为另一个字符串.您需要一直使用数据,直到需要将其显示在屏幕上为止.在这种情况下,请使用System.DateTime.ToString函数之一.是的,就这么简单.

—SA
You have a number of answers, but your root problem is different. It try to work with string representation of data, instead of data itself. This is wrong. Work with data, in this case, System.DateTime data. You don''t usually need to convert one string into another. You need to work with data all the time until you need to present it on screen. In that case, use one of the System.DateTime.ToString functions. Yes, as simple as that.

—SA


这篇关于将dd/mm/yy转换为dd-mm-yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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