如何将字符串格式化为区域日期时间 [英] How To I Format A String To Regional Datetime

查看:120
本文介绍了如何将字符串格式化为区域日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我再次陷入困境,我希望你们能再次帮助我,这对我来说仍然是一个学习曲线,但在这里是我有什么



我必须使用我的系统日期(我正在写的系统)

所以我这样返回它所以我可以更轻松地使用它

  string  sToday = Globals.Instance.CurrentFSVD; 
string sY yesterday = Globals.Instance.PreviousFSVD;





所以它返回一个看起来像这样的字符串

   20080521 





我需要将此字符串格式化为

 DateTime 



所以我可以将它作为一个值提供给我的日期选择器,并使用它来设置我的日期选择器的最大值,最重要的是我需要它来格式化该日期根据我的电脑的区域设置



我真的很难做到这一点



我试过这样做

  string  sToday =  string  .Format(  {0:d},Globals.Instance.CurrentFSVD); 



  string  sY yesterday =  string  .Format(  {0:d},Globals.Instance.PreviousFSVD); 





只是为了让我能以正确的格式获取字符串但遗憾的是仍然只收到与我尝试的第一个结果相同

   20080521 



然后我试了这个



今天的DateTime = DateTime.Parse(sToday); 
DateTime昨天= DateTime.Parse(sY yesterday);





我似乎无法弄明白。





现在我的代码看起来像这样



 尝试 
{
string sToday = string .Format( {0:d},Globals。 Instance.CurrentFSVD);
string sY yesterday = string .Format( {0:d},Globals.Instance.PreviousFSVD);
今日DateTime = DateTime.Parse(sToday);
DateTime昨天= DateTime.Parse(sY yesterday);
}
catch (Exception ev)
{
throw ev;
}





我相信可以在字符串中插入我需要的东西,以便我可以转换它,但是在这种情况下,这不是我需要的,我需要它根据区域设置格式化



我很抱歉,如果这个问题不具备意义你们中的一些人,但这是我解释它的最佳方式,所以,如果你们能帮助我真的很感激

解决方案

首先查看任何类型 Globals.Instance.CurrentFSVD Globals.Instance.PreviousFSVD 是:可能是他们是明智的类型,如已经是DateTime,或者它们是一种很容易转换为DateTime而没有基于字符串的日期格式的中间阶段的类型。



当然,我们无法帮助你是因为我们不知道它们是什么:它们是代码中的变量,而不是.NET标准的东西,数字20080521并不意味着很多在某种形式的上下文中可以理解它。



所以看看定义,找出它们是什么类型,然后查看该类以查看它的转换提供。

Hi Guys,

I'm stuck again, and i hope that you guys can help me out again, this is still a learning curve for me but here is what i have

I have to use my systems date(the system i'm writing)
so i return it like this so that i can use it easier

string sToday = Globals.Instance.CurrentFSVD;
string sYesterday = Globals.Instance.PreviousFSVD;



so then it return a string that looks like this

"20080521"



I need to format this string to a

DateTime


so that i can give it to my Date Picker as a value and also use it to set the MAX of my Date Picker, and on top of that i need it to format that date according to my PC's Regional settings

I am really struggling to get this right

I have tried to do this

string sToday = string.Format("{0:d}", Globals.Instance.CurrentFSVD);


string sYesterday = string.Format("{0:d}", Globals.Instance.PreviousFSVD);



Just so that i can get the string in the right format but unfortunately still only received the same results as the first one i tried

"20080521"


and then i tried this

DateTime today = DateTime.Parse(sToday);
DateTime yesterday = DateTime.Parse(sYesterday);



I just can't seem to figure it out.


so now my code looks like this

 try 
{
string sToday = string.Format("{0:d}", Globals.Instance.CurrentFSVD);
string sYesterday = string.Format("{0:d}", Globals.Instance.PreviousFSVD);
DateTime today = DateTime.Parse(sToday);
DateTime yesterday = DateTime.Parse(sYesterday);
}
catch (Exception ev)
{		
throw ev;
}



I believe that it is possible to insert what i need into the string so that i can convert it, but in this case that is not what i need, i need it to format according to the Regional Settings

I am sorry if this question doesn't make allot of sense to some of you, but this is my best way of explaining it, so please if you can help i would really appreciate it

解决方案

Start by looking at whatever type Globals.Instance.CurrentFSVD and Globals.Instance.PreviousFSVD are: The chances are that either they are a "sensible" type such as a DateTime already, or they are a type which readily converts to a DateTime without the intermediate stage of a string based date format.

Certainly, we can't help you because we have no idea what they are: they are variables in your code, not .NET standard stuff, and the number 20080521 doesn't mean a lot without some form of context in which to understand it.

So look at the definition, find out what type they are, and look at that class to see what conversions it provides.


这篇关于如何将字符串格式化为区域日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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