来自不同国家的日期 [英] date from different countries

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

问题描述

我有一个在机器上运行时100%完美的应用程序

设置为英语(美国),但当我将其更改为西班牙语时

(墨西哥) ),日期开始让我适合。


原因是美国是mm / dd / yyyy,而墨西哥是dd / mm / yyyy。因此,将

计算机设置为墨西哥,任何标准CDATE功能都将返回

dd / mm / yyyy设置中的日期,因为这是计算机的

设置为。


我希望能够以dd / mm / yyyy格式输入日期但返回真实的

日期,但格式为mm / dd / yyyy,类似于CDate。我不想要一个

字符串,我想要一个真实的约会。


我写了一个例程,改变了订单以获得格式,但它

是一个字符串。当我尝试将其更改为日期时,Cdate例程

会更改格式或错误,因为它正在尝试更改

格式。


昏暗的垃圾当日期

Dim sjunk As String

junk =#1/1/1901#

试试

如果不是IsDBNull(in_object)那么

sjunk = DRStr(in_object)

如果

System.Globalization.DateTimeFormatInfo .CurrentInf o.ShortDatePattern<>

" M / d / yyyy"然后

''不是USA

Dim xpattern As String

xpattern =

System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern

如果Mid(xpattern,1,1)=" d"然后

''是第一天

Dim ddate As String

Dim xflds()As String

xflds =拆分(DRStr(sjunk),/)

ddate = xflds(1)& " /" &安培; xflds(0)& " /" &安培; xflds(2)

sjunk = ddate

结束如果

结束如果

junk = CDate(sjunk)

Else

junk = CDate(in_object)

结束如果

捕获o除非作为例外

''ShowMessage(oExcept.Message)

结束尝试

返回垃圾


语句junk = cdate(sjunk)是什么把日期改回到

计算器上的全局设置。


任何帮助将不胜感激。我真的希望CDate允许我有一个论点,因为全球化设置允许我

强制我想要的日期设置。别忘了,FORMAT

命令返回一个字符串,而不是日期。


提前谢谢。


Darin


***通过开发人员指南 http: //www.developersdex.com ***

I have an applicatoin that works 100% perfect when running on a machine
setup for English (United States), but when I change it to Spanish
(Mexico), the dates start giving me fits.

THe reason is USA is mm/dd/yyyy and mexico is dd/mm/yyyy. So, with the
computer set to mexico, any standard CDATE function is going to return
the date in the dd/mm/yyyy setting since that is what the computer is
set to.

I want to be able to enter a date in dd/mm/yyyy format but return a true
date, but in the format mm/dd/yyyy, similar to CDate. I don''t want a
string, I want a real date.

I wrote a routine that changed the order to get it in the format, but it
is a string. When I then try to change it to a date, the Cdate routine
either changes the format or errors because it is trying to change the
format.

Dim junk As Date
Dim sjunk As String
junk = #1/1/1901#
Try
If Not IsDBNull(in_object) Then
sjunk = DRStr(in_object)
If
System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern <>
"M/d/yyyy" Then
'' not USA
Dim xpattern As String
xpattern =
System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern
If Mid(xpattern, 1, 1) = "d" Then
'' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sjunk), "/")
ddate = xflds(1) & "/" & xflds(0) & "/" & xflds(2)
sjunk = ddate
End If
End If
junk = CDate(sjunk)
Else
junk = CDate(in_object)
End If
Catch oExcept As Exception
''ShowMessage(oExcept.Message)
End Try
Return junk

The statement junk=cdate(sjunk) is what is changing the date back into
the global settings on the comptuer.

Any help would be greatly appreciated. I really wish CDate would allow
me to have an argument as the globalization settings allowing me to
force what setting I want the date to be. Don''t forget, the FORMAT
command returns a string, not a date.

Thanks in advance.

Darin

*** Sent via Developersdex http://www.developersdex.com ***

推荐答案

不同文化中的日期可能会成为一个雷区。


只要日期部分日期输入4位数字,那部分

通常不是问题。


当您需要分析用户输入的日期和月份部件时会出现问题。


如果当天部分为13或更高然后很容易识别为当天

,因为月份只有1到12个。


如果日期部分和月份部分相同则有没问题

因为01/01/2006很容易识别dd / MM / yyyy或MM / dd / yyyy

表格ats。


如果日期部分是1到12并且它与月份部分不同那么你

有一些问题。例如。是01/02/2007意味着代表2月1日

或1月2日。


如果给用户的指示是输入日期作为dd / MM / yyyy(或

MM / dd / yyyy)无论文化如何都可以使用:


Dim _d As DateTime = DateTime.ParseExact (值,dd / MM / yyyy)

''或MM / dd / yyyy根据需要


如果对用户的指示是根据文件

输入日期然后可以使用的机器:


Dim _d As DateTime = DateTime.ParseExact(value,

DateTimeFormatInfo.CurrentInfo.ShortDatePattern)


不幸的是,任何给定的机器都可以将它的ShortDatePattern设置为

不同于默认模式机器文化的模式。


克服这个问题的一种方法是将日期字符串的部分填充到

确保日期和月份部件是预期长度:


Dim _p arts As String()= value.Split(" /" c)

_parts(0)= Parts(0).PadLeft(2," 0")

_parts(1)= Parts(1).PadLeft(2," 0")

value = String.Join("。",_ parts)

>
对于value =" 1/1 / 2007",值将被修改为01/01/2007。这将是

现在可以使用:


Dim _d As DateTime = DateTime.ParseExact(value,dd / MM / yyyy)

" Darin" < darin_nospam @nospameverwrote in message

news:ei ************** @ TK2MSFTNGP04.phx.gbl ...
Dates in different cultures can be a bit of a minefield.

As long as the year part of the date is entered with 4 digits then that part
is not generally an issue.

The problems arise when you need to analyse what the user has entered for
the day and month parts.

If the day part is 13 or greater then that is easily identifiable as the day
because months are only 1 to 12.

If the day part and the month part are the same then there is no problem
because 01/01/2006 is easily identifiable in either dd/MM/yyyy or MM/dd/yyyy
formats.

If the day part is 1 to 12 and it is not the same as the month part then you
have some issues. E.G. Is 01/02/2007 meant to represent the 1st of February
or the 2nd of January.

If the instruction to the user is to enter the date as dd/MM/yyyy (or
MM/dd/yyyy) regardless of culture then one can use:

Dim _d As DateTime = DateTime.ParseExact(value, "dd/MM/yyyy")
'' or MM/dd/yyyy as required

If the instruction to the user is to enter the date as per the culture of
the machine then one can use:

Dim _d As DateTime = DateTime.ParseExact(value,
DateTimeFormatInfo.CurrentInfo.ShortDatePattern)

Unfortunately, any given machine can have it''s ShortDatePattern set to a
different pattern than the default pattern for the culture of the machine.

One way of overcoming this issue is to pad the parts of the date string to
make sure the day and month parts are the expected length:

Dim _parts As String() = value.Split("/"c)
_parts(0) = Parts(0).PadLeft(2, "0")
_parts(1) = Parts(1).PadLeft(2, "0")
value = String.Join(".", _parts)

For value = "1/1/2007", value will be modified to "01/01/2007" which will
now work with:

Dim _d As DateTime = DateTime.ParseExact(value, "dd/MM/yyyy")
"Darin" <darin_nospam@nospameverwrote in message
news:ei**************@TK2MSFTNGP04.phx.gbl...

>我有一个在机器上运行时100%完美的应用程序

设置为英语(美国),但当我将其更改为西班牙语时

(墨西哥),日期开始让我适合。


原因是美国是mm / dd / yyyy,墨西哥是dd / mm / yyyy。因此,将

计算机设置为墨西哥,任何标准CDATE功能都将返回

dd / mm / yyyy设置中的日期,因为这是计算机的

设置为。


我希望能够以dd / mm / yyyy格式输入日期但返回真实的

日期,但格式为mm / dd / yyyy,类似于CDate。我不想要一个

字符串,我想要一个真实的约会。


我写了一个例程,改变了订单以获得格式,但它

是一个字符串。当我尝试将其更改为日期时,Cdate例程

会更改格式或错误,因为它正在尝试更改

格式。


昏暗的垃圾当日期

Dim sjunk As String

junk =#1/1/1901#

试试

如果不是IsDBNull(in_object)那么

sjunk = DRStr(in_object)

如果

System.Globalization.DateTimeFormatInfo .CurrentInf o.ShortDatePattern<>

" M / d / yyyy"然后

''不是USA

Dim xpattern As String

xpattern =

System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern

如果Mid(xpattern,1,1)=" d"然后

''是第一天

Dim ddate As String

Dim xflds()As String

xflds =拆分(DRStr(sjunk),/)

ddate = xflds(1)& " /" &安培; xflds(0)& " /" &安培; xflds(2)

sjunk = ddate

结束如果

结束如果

junk = CDate(sjunk)

Else

junk = CDate(in_object)

结束如果

捕获o除非作为例外

''ShowMessage(oExcept.Message)

结束尝试

返回垃圾


语句junk = cdate(sjunk)是什么把日期改回到

计算器上的全局设置。


任何帮助将不胜感激。我真的希望CDate允许我有一个论点,因为全球化设置允许我

强制我想要的日期设置。别忘了,FORMAT

命令返回一个字符串,而不是日期。


提前谢谢。


Darin


***通过开发人员指南 http: //www.developersdex.com ***
>I have an applicatoin that works 100% perfect when running on a machine
setup for English (United States), but when I change it to Spanish
(Mexico), the dates start giving me fits.

THe reason is USA is mm/dd/yyyy and mexico is dd/mm/yyyy. So, with the
computer set to mexico, any standard CDATE function is going to return
the date in the dd/mm/yyyy setting since that is what the computer is
set to.

I want to be able to enter a date in dd/mm/yyyy format but return a true
date, but in the format mm/dd/yyyy, similar to CDate. I don''t want a
string, I want a real date.

I wrote a routine that changed the order to get it in the format, but it
is a string. When I then try to change it to a date, the Cdate routine
either changes the format or errors because it is trying to change the
format.

Dim junk As Date
Dim sjunk As String
junk = #1/1/1901#
Try
If Not IsDBNull(in_object) Then
sjunk = DRStr(in_object)
If
System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern <>
"M/d/yyyy" Then
'' not USA
Dim xpattern As String
xpattern =
System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern
If Mid(xpattern, 1, 1) = "d" Then
'' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sjunk), "/")
ddate = xflds(1) & "/" & xflds(0) & "/" & xflds(2)
sjunk = ddate
End If
End If
junk = CDate(sjunk)
Else
junk = CDate(in_object)
End If
Catch oExcept As Exception
''ShowMessage(oExcept.Message)
End Try
Return junk

The statement junk=cdate(sjunk) is what is changing the date back into
the global settings on the comptuer.

Any help would be greatly appreciated. I really wish CDate would allow
me to have an argument as the globalization settings allowing me to
force what setting I want the date to be. Don''t forget, the FORMAT
command returns a string, not a date.

Thanks in advance.

Darin

*** Sent via Developersdex http://www.developersdex.com ***



只需使用通用日期ISO 8601格式


YYYYMMDD YYYY-MM-DD

hhmmss hh:mm:ss

这应该适用于任何系统,无论是否有本地设置< br $>

问候


米歇尔


" Darin" < darin_nospam @nospameverschreef in bericht

news:ei ************** @ TK2MSFTNGP04.phx.gbl ...
Just use universal date ISO 8601 formats

YYYYMMDD YYYY-MM-DD

hhmmss hh:mm:ss
this should work on any system regardless of there local settings

regards

Michel

"Darin" <darin_nospam@nospameverschreef in bericht
news:ei**************@TK2MSFTNGP04.phx.gbl...

>我有一个在机器上运行时100%完美的应用程序

设置为英语(美国),但当我将其更改为西班牙语时

(墨西哥),日期开始让我适合。


原因是美国是mm / dd / yyyy,墨西哥是dd / mm / yyyy。因此,将

计算机设置为墨西哥,任何标准CDATE功能都将返回

dd / mm / yyyy设置中的日期,因为这是计算机的

设置为。


我希望能够以dd / mm / yyyy格式输入日期但返回真实的

日期,但格式为mm / dd / yyyy,类似于CDate。我不想要一个

字符串,我想要一个真实的约会。


我写了一个例程,改变了订单以获得格式,但它

是一个字符串。当我尝试将其更改为日期时,Cdate例程

会更改格式或错误,因为它正在尝试更改

格式。


昏暗的垃圾当日期

Dim sjunk As String

junk =#1/1/1901#

试试

如果不是IsDBNull(in_object)那么

sjunk = DRStr(in_object)

如果

System.Globalization.DateTimeFormatInfo .CurrentInf o.ShortDatePattern<>

" M / d / yyyy"然后

''不是USA

Dim xpattern As String

xpattern =

System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern

如果Mid(xpattern,1,1)=" d"然后

''是第一天

Dim ddate As String

Dim xflds()As String

xflds =拆分(DRStr(sjunk),/)

ddate = xflds(1)& " /" &安培; xflds(0)& " /" &安培; xflds(2)

sjunk = ddate

结束如果

结束如果

junk = CDate(sjunk)

Else

junk = CDate(in_object)

结束如果

捕获o除非作为例外

''ShowMessage(oExcept.Message)

结束尝试

返回垃圾


语句junk = cdate(sjunk)是什么把日期改回到

计算器上的全局设置。


任何帮助将不胜感激。我真的希望CDate允许我有一个论点,因为全球化设置允许我

强制我想要的日期设置。别忘了,FORMAT

命令返回一个字符串,而不是日期。


提前谢谢。


Darin


***通过开发人员指南 http: //www.developersdex.com ***
>I have an applicatoin that works 100% perfect when running on a machine
setup for English (United States), but when I change it to Spanish
(Mexico), the dates start giving me fits.

THe reason is USA is mm/dd/yyyy and mexico is dd/mm/yyyy. So, with the
computer set to mexico, any standard CDATE function is going to return
the date in the dd/mm/yyyy setting since that is what the computer is
set to.

I want to be able to enter a date in dd/mm/yyyy format but return a true
date, but in the format mm/dd/yyyy, similar to CDate. I don''t want a
string, I want a real date.

I wrote a routine that changed the order to get it in the format, but it
is a string. When I then try to change it to a date, the Cdate routine
either changes the format or errors because it is trying to change the
format.

Dim junk As Date
Dim sjunk As String
junk = #1/1/1901#
Try
If Not IsDBNull(in_object) Then
sjunk = DRStr(in_object)
If
System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern <>
"M/d/yyyy" Then
'' not USA
Dim xpattern As String
xpattern =
System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern
If Mid(xpattern, 1, 1) = "d" Then
'' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sjunk), "/")
ddate = xflds(1) & "/" & xflds(0) & "/" & xflds(2)
sjunk = ddate
End If
End If
junk = CDate(sjunk)
Else
junk = CDate(in_object)
End If
Catch oExcept As Exception
''ShowMessage(oExcept.Message)
End Try
Return junk

The statement junk=cdate(sjunk) is what is changing the date back into
the global settings on the comptuer.

Any help would be greatly appreciated. I really wish CDate would allow
me to have an argument as the globalization settings allowing me to
force what setting I want the date to be. Don''t forget, the FORMAT
command returns a string, not a date.

Thanks in advance.

Darin

*** Sent via Developersdex http://www.developersdex.com ***



Darin,

没有#1-1-1900#或其他来自

调试器的表示。


DateTimes永远是从一个不真实的开始时刻(不同于

dotNet和数据库),它永远根据用户的文化设置来计算日期和时间。


我经常更喜欢CDate,因为它很自动。


Cor


" Darin" < darin_nospam @nospameverschreef in bericht

news:ei ************** @ TK2MSFTNGP04.phx.gbl ...
Darin,

There is no #1-1-1900# or whatever that is the representation from the
debugger.

DateTimes are forever ticks starting at a not real moment (different for
dotNet and databases), which forever calculate the date and times based on
the culture settings of the user.

I prefer very often the CDate because it does a lot automatic.

Cor

"Darin" <darin_nospam@nospameverschreef in bericht
news:ei**************@TK2MSFTNGP04.phx.gbl...

>我有一个在机器上运行时100%完美的应用程序

设置为英语(美国),但当我将其更改为西班牙语时

(墨西哥),日期开始让我适合。


原因是美国是mm / dd / yyyy,墨西哥是dd / mm / yyyy。因此,将

计算机设置为墨西哥,任何标准CDATE功能都将返回

dd / mm / yyyy设置中的日期,因为这是计算机的

设置为。


我希望能够以dd / mm / yyyy格式输入日期但返回真实的

日期,但格式为mm / dd / yyyy,类似于CDate。我不想要一个

字符串,我想要一个真实的约会。


我写了一个例程,改变了订单以获得格式,但它

是一个字符串。当我尝试将其更改为日期时,Cdate例程

会更改格式或错误,因为它正在尝试更改

格式。


昏暗的垃圾当日期

Dim sjunk As String

junk =#1/1/1901#

试试

如果不是IsDBNull(in_object)那么

sjunk = DRStr(in_object)

如果

System.Globalization.DateTimeFormatInfo .CurrentInf o.ShortDatePattern<>

" M / d / yyyy"然后

''不是USA

Dim xpattern As String

xpattern =

System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern

如果Mid(xpattern,1,1)=" d"然后

''是第一天

Dim ddate As String

Dim xflds()As String

xflds =拆分(DRStr(sjunk),/)

ddate = xflds(1)& " /" &安培; xflds(0)& " /" &安培; xflds(2)

sjunk = ddate

结束如果

结束如果

junk = CDate(sjunk)

Else

junk = CDate(in_object)

结束如果

捕获o除非作为例外

''ShowMessage(oExcept.Message)

结束尝试

返回垃圾


语句junk = cdate(sjunk)是什么把日期改回到

计算器上的全局设置。


任何帮助将不胜感激。我真的希望CDate允许我有一个论点,因为全球化设置允许我

强制我想要的日期设置。别忘了,FORMAT

命令返回一个字符串,而不是日期。


提前谢谢。


Darin


***通过开发人员指南 http: //www.developersdex.com ***
>I have an applicatoin that works 100% perfect when running on a machine
setup for English (United States), but when I change it to Spanish
(Mexico), the dates start giving me fits.

THe reason is USA is mm/dd/yyyy and mexico is dd/mm/yyyy. So, with the
computer set to mexico, any standard CDATE function is going to return
the date in the dd/mm/yyyy setting since that is what the computer is
set to.

I want to be able to enter a date in dd/mm/yyyy format but return a true
date, but in the format mm/dd/yyyy, similar to CDate. I don''t want a
string, I want a real date.

I wrote a routine that changed the order to get it in the format, but it
is a string. When I then try to change it to a date, the Cdate routine
either changes the format or errors because it is trying to change the
format.

Dim junk As Date
Dim sjunk As String
junk = #1/1/1901#
Try
If Not IsDBNull(in_object) Then
sjunk = DRStr(in_object)
If
System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern <>
"M/d/yyyy" Then
'' not USA
Dim xpattern As String
xpattern =
System.Globalization.DateTimeFormatInfo.CurrentInf o.ShortDatePattern
If Mid(xpattern, 1, 1) = "d" Then
'' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sjunk), "/")
ddate = xflds(1) & "/" & xflds(0) & "/" & xflds(2)
sjunk = ddate
End If
End If
junk = CDate(sjunk)
Else
junk = CDate(in_object)
End If
Catch oExcept As Exception
''ShowMessage(oExcept.Message)
End Try
Return junk

The statement junk=cdate(sjunk) is what is changing the date back into
the global settings on the comptuer.

Any help would be greatly appreciated. I really wish CDate would allow
me to have an argument as the globalization settings allowing me to
force what setting I want the date to be. Don''t forget, the FORMAT
command returns a string, not a date.

Thanks in advance.

Darin

*** Sent via Developersdex http://www.developersdex.com ***



这篇关于来自不同国家的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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