chnage date from“22-12-2012”至“12-22-2012” [英] chnage date from "22-12-2012" to "12-22-2012"

查看:83
本文介绍了chnage date from“22-12-2012”至“12-22-2012”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net中将日期格式从dd-MM-yy改为MM-dd-yy



我有约会22-12-2012

i想换成12-22-2012



当我尝试转换它时会给我错误



字符串未被识别为有效的DateTime。

解决方案

 CultureInfo ci =  new  CultureInfo(  gu-IN); 
Datetime strDate = Datetime.ParseExact( 22-12-2012,ci);





谢谢


首先,我认为你真的不需要兑换。您不应该使用数据的字符串表示而不是数据。在您的情况下,您应该使用 System.DateTime 而不是代表它的字符串。只有你需要在屏幕上显示数据,你使用 System.DateTime 方法之一,然后才选择格式。请参阅:

http://msdn.microsoft.com/en -us / library / system.datetime.aspx [ ^ ],读取每个 ToString 方法。



格式化请请参阅

http://msdn.microsoft.com/en-us/library /az4se3k1.aspx [ ^ ],

http://msdn.microsoft.com/en-us /library/8kb3ddd4.aspx [ ^ ]。



另一种格式化方式是选择合适的文化,例如:

http://msdn.microsoft.com/en-us/library/ht77y576.aspx [ ^ ]。



In在极少数情况下,您需要解释某种格式的字符串。即使这样也可以简化,因为在SQL中你应该更好地使用 DATE 类型与特定格式无关,而不是字符串。但是如果你需要,你应该解析一个字符串到 System.DateTime ,所以看看所有方法 Parse ParseExact TryParse TryParseExact



当然,使用引用的方法,您可以根据需要转换,但转换方法是错误的。



-SA


 DateTime _date = Convert.ToDateTime(  22-12-2012); 
string str = _date.ToString( MM-DD-YYYY);


how to chnage date format from dd-MM-yy to MM-dd-yy in asp.net

I have date "22-12-2012"
i want to change into "12-22-2012"

when i try to convert it gives me error

String was not recognized as a valid DateTime.

解决方案

CultureInfo ci=new CultureInfo("gu-IN");
Datetime strDate= Datetime.ParseExact("22-12-2012",ci);



Thanks


First of all, I don''t think you really need to "convert". You should not work with string representation of data instead of data. In your case, you should work with System.DateTime instead of strings representing it. Only of you need to show the data on screen, you use one of System.DateTime methods, and only then you choose the format. Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^], read on every ToString method.

For formatting, please see
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Another way of formatting is through choosing appropriate culture, see, for example:
http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^].

In rare cases, you need to interpret string from certain format. Even this can be simplified, because, say, in SQL you should better use DATE type not tied to a particular format, in contrast to strings. But in case you need, you should parse a string into System.DateTime, so see all the methods Parse, ParseExact, TryParse and TryParseExact.

Of course, using the methods referenced, you can "convert" as you wanted, but "convert" approach is wrong.

—SA


DateTime _date = Convert.ToDateTime("22-12-2012");
string str = _date.ToString("MM-dd-yyyy");


这篇关于chnage date from“22-12-2012”至“12-22-2012”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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