如何在asp.net中更改日期格式中的字符串更改 [英] how to change string in Date format change in asp.net

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

问题描述

大家好,



我有字符串,我已将日期定为

string date =07/02/2011作为dd / mm / yyyy格式。

当我以日期格式转换它时它在07/02/2011中更改但我必须在02/07/2011更改它。



请帮帮我

谢谢

Mohd Wasif

解决方案

< blockquote>这将帮助你



 string date =07/02/2011
Respose.Write(string.format( {0:dd / MM / yyyy},日期));


这不是改变07/02/2011因为你刚刚把一个字符串转换为日期,但这将是一个日期的默认表示。表示为MM / dd / yyyy,由于可能在解释中出现错误而无法在逻辑上进行更改,因为不清楚07是月份还是日期(这也是给定日期的02)。



我自己来自荷兰,代表那里使用短划线( - )而不是斜线(/),日期从日而不是月开始。所以07/02/2011将成为02-07-2011。斜杠和短划线清楚地表明前两个值代表的是什么(日或月)。没有它,除非当天高于13,因为没有第13个月,所以真的没有办法知道。



所以,请你解释一下原因是什么反转日和月?如果你真的想,你可以使用Split方法,如:

 string [] split = date.Split(new Char [] {'\\'} ); 
//您应该检查数组的下限和上限
string newDate = split [1] + split [0] + split [2];





你也可以使用自定义格式:

 string newDate = String.Format({0:dd / MM / yyyy},dt); // dt是DateTime类型





祝你好运!


你可以从这里得到帮助链接:



http://www.csharp -examples.net/string-format-datetime/ [ ^ ]

Hi all,

I have string in that I have set date as
string date="07/02/2011" as dd/mm/yyyy format.
when I am converting it in date format it's changing in "07/02/2011" but i have to change it in 02/07/2011.

Please help me
Thanks
Mohd Wasif

解决方案

This will help you

string date="07/02/2011"
Respose.Write(string.format("{0:dd/MM/yyyy}",date));


It isn't "changing in" "07/02/2011" because you just did cast a string to date, but that would be the default representation of a date. The representation is "MM/dd/yyyy" and cannot logically be changed due to possible errors in interpretation because it is unclear if 07 is the month or the day (this also goes for 02 in the date given).

I myself am from the netherlands and the representation there is using a dash(-) instead of a slash(/) and the date is starting with the day instead of the month. So "07/02/2011" would become "02-07-2011". The slash and dash keep it clear what the first two values represent (either day or month). Without that there is really no way to know except when the day is above 13 because there is no 13th month.

So, could you please explain what the reason is to reverse day and month? If you really want to, you can just use the Split method like:

string [] split = date.Split(new Char [] {'\\'});
// You should need to check the lower and upper bound of the array 
string newDate = split[1] + split[0] + split[2];



You could also use a custom formatting:

string newDate = String.Format("{0:dd/MM/yyyy}", dt); // dt is of type DateTime



Good luck!


You can get help from this link:

http://www.csharp-examples.net/string-format-datetime/[^]


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

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