日期时间格式问题 [英] Date Time Format Problem

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

问题描述



我有一个程序需要以dd / MM / yyyy格式获取日期

(例如24/7/2005)


但是当我使用DateTime.Now()。ToShortDatetime()时,日期又回来了

MM / dd / yyyy(例如7/24/2005)


这会导致很多问题


我想知道如何更改.NET区域设置以便

日期将通过更改计算机设置或代码来返回dd / MM / yyyy




解决方案
" okaminer" < OK ******* @ yahoo.com> schrieb


我有一个程序需要以
dd / MM / yyyy格式获取日期(例如24/7/2005)
<但是当我使用DateTime.Now()。ToShortDatetime()时,日期会回来
为MM / dd / yyyy(例如7/24/2005)

这会造成很多问题

我想知道如何更改.NET区域设置,以便
日期将以dd / MM / yyyy
的形式返回,或者通过更改计算机设置或按代码



您应该保留计算机设置,因为如果您更改它们,用户可能不会被打开。

而不是ToShortDatetime,使用ToString(格式)。见< f1>可用的

格式,例如。 dd \ / M \ / yyyy或d \ / M \ / yyyy


Armin


感谢您的回复


我不介意更改计算机设置,因为这不会在

客户端计算机上运行但是它是一个web应用程序,它将运行在

服务器/

我也使用函数ToString(dd / MM / yyyy),但当我尝试为了
将这个值放回DateTime参数,计算机出错了

,因为他认为在24/7/2005中24是月(并且明确这个

是一个错误,因为没有月份24)


所以我真的需要知道如何从代码更改为计算机设置

或来自控制面板


再次感谢


" okaminer" < OK ******* @ yahoo.com> schrieb

感谢您的回复

我不介意更改计算机设置,因为这不会在客户端计算机上运行
但是它是一个Web应用程序将会在
服务器上运行/
我也在使用函数ToString(dd / MM / yyyy),但是当我尝试将这个值放回DateTime参数时,计算机出现错误,因为他认为在24/7/2005期间24是月(并且明确这是一个错误,因为没有月份24)


让我澄清一下:你的问题不是将它转换为字符串,而是从

字符串转换为日期/时间,对吧?


你是怎么试的?把它转换为DateTime?如果字符串不应格式化为系统设置中指定的

,则从字符串中提取

值非常容易。示例:


专用共享函数GetDateTimeFromDMYString(_

ByVal s As String,_

可选ByVal Seperator As Char =" /" c)_

作为日期


昏暗部分为字符串()

parts = s.Split(Seperator)

返回新的DateTime(_

Integer.Parse(parts(0)),_

Integer.Parse(parts(1)), _

Integer.Parse(部分(2))_



结束功能


所以我真的需要知道如何从
代码或控制面板更改为计算机设置




对不起,我不知道。可能使用API​​函数。

Armin


Hi
I have a program which need to get the date in the format of dd/MM/yyyy
(example 24/7/2005)

but when I use DateTime.Now().ToShortDatetime() the date come back as
MM/dd/yyyy (example 7/24/2005)

This cause much problems

I would like to know how to change the .NET regional setting so the
date will come back as dd/MM/yyyy

either by changing the computer settings or by code

解决方案

"okaminer" <ok*******@yahoo.com> schrieb

Hi
I have a program which need to get the date in the format of
dd/MM/yyyy (example 24/7/2005)

but when I use DateTime.Now().ToShortDatetime() the date come back
as MM/dd/yyyy (example 7/24/2005)

This cause much problems

I would like to know how to change the .NET regional setting so the
date will come back as dd/MM/yyyy

either by changing the computer settings or by code


You should leave the computer settings because the user will probably not be
amused if you change them.

Instead of ToShortDatetime, use ToString(format). See <f1> for available
formats, eg. "dd\/M\/yyyy", or "d\/M\/yyyy"

Armin


Thanks for replying

I don''t mind changing the computer settings since this will not run on
client compter but it is a web application which will run on the
server/
Also I am using the function ToString("dd/MM/yyyy"), but when I try to
put this value back to a DateTime parameter, the computer gets an error
because he thinks that in 24/7/2005 the 24 is Month (and clearlly this
is an error because there is no month 24)

So I really need to know how to change to computer setting from code
or from control panel

thanks again


"okaminer" <ok*******@yahoo.com> schrieb

Thanks for replying

I don''t mind changing the computer settings since this will not run
on client compter but it is a web application which will run on the
server/
Also I am using the function ToString("dd/MM/yyyy"), but when I try
to put this value back to a DateTime parameter, the computer gets an
error because he thinks that in 24/7/2005 the 24 is Month (and
clearlly this is an error because there is no month 24)
Let me clarify: Your problem is not converting it to a string but from a
string to date/time, right?

How did you try to convert it to DateTime? It''s pretty easy to extract the
values from the string if the string is not supposed to be formatted as
specified in the system''s settings. Example:

Private Shared Function GetDateTimeFromDMYString( _
ByVal s As String, _
Optional ByVal Seperator As Char = "/"c) _
As Date

Dim parts As String()
parts = s.Split(Seperator)
Return New DateTime( _
Integer.Parse(parts(0)), _
Integer.Parse(parts(1)), _
Integer.Parse(parts(2)) _
)
End Function

So I really need to know how to change to computer setting from
code or from control panel



Sorry, I don''t know. Probably using API functions.
Armin


这篇关于日期时间格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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