473,763 Members | 3,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

date from different countries

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_obj ect) Then
sjunk = DRStr(in_object )
If
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern <>
"M/d/yyyy" Then
' not USA
Dim xpattern As String
xpattern =
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern
If Mid(xpattern, 1, 1) = "d" Then
' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sju nk), "/")
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(oE xcept.Message)
End Try
Return junk

The statement junk=cdate(sjun k) 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 ***
Jan 18 '07 #1
21 3386
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.ParseE xact(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.ParseE xact(value,
DateTimeFormatI nfo.CurrentInfo .ShortDatePatte rn)

Unfortunately, any given machine can have it's ShortDatePatter n 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).PadLef t(2, "0")
_parts(1) = Parts(1).PadLef t(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.ParseE xact(value, "dd/MM/yyyy")
"Darin" <darin_nospam@n ospameverwrote in message
news:ei******** ******@TK2MSFTN GP04.phx.gbl...
>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_obj ect) Then
sjunk = DRStr(in_object )
If
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern <>
"M/d/yyyy" Then
' not USA
Dim xpattern As String
xpattern =
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern
If Mid(xpattern, 1, 1) = "d" Then
' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sju nk), "/")
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(oE xcept.Message)
End Try
Return junk

The statement junk=cdate(sjun k) 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 ***

Jan 18 '07 #2
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@n ospameverschree f in bericht
news:ei******** ******@TK2MSFTN GP04.phx.gbl...
>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_obj ect) Then
sjunk = DRStr(in_object )
If
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern <>
"M/d/yyyy" Then
' not USA
Dim xpattern As String
xpattern =
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern
If Mid(xpattern, 1, 1) = "d" Then
' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sju nk), "/")
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(oE xcept.Message)
End Try
Return junk

The statement junk=cdate(sjun k) 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 ***

Jan 18 '07 #3
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@n ospameverschree f in bericht
news:ei******** ******@TK2MSFTN GP04.phx.gbl...
>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_obj ect) Then
sjunk = DRStr(in_object )
If
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern <>
"M/d/yyyy" Then
' not USA
Dim xpattern As String
xpattern =
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern
If Mid(xpattern, 1, 1) = "d" Then
' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sju nk), "/")
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(oE xcept.Message)
End Try
Return junk

The statement junk=cdate(sjun k) 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 ***

Jan 18 '07 #4
Thanks.

I entered in today's date (18/1/2007) and then used:

DateTime.ParseE xact(DRStr(in_o bject), "MM/dd/yyyy",
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo)

and it received an error that the string isn't a valid date (my computer
is set for Spanish(Mexico) so the date is dd/mm/yyyy)

Any ideas.

Darin

*** Sent via Developersdex http://www.developersdex.com ***
Jan 18 '07 #5
Ummmmmm.... yeah!

18/1/2007 is clearly supposed to represent the 18th of January because 18 is
greater than the maximum number for month (12).

DateTime.ParseE xact(DRStr(in_o bject), "MM/dd/yyyy",
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo)

specifies that the result of DRStr(in_object ) MUST be in MM/dd/yyyy format.
If it is not in that EXACT format then it will throw an exception (as you
have observed).

18/1/2007 is actually dd/M/yyyy format.

Note that everything I said in my previous post was in context of your
statement that you wouldb entering dates in dd/MM/yyyy format (3rd
paragraph, 1st sentence).

For that you need:

DateTime.ParseE xact(DRStr(in_o bject), "dd/MM/yyyy",
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo)

Note that each placeholder in the format string means a digit, so dd means
01 and not 1 and MM means 01 and not 1. Thus 18/01/2007 matches dd/MM/yyyy
format whereas 18/1/2007 matches dd/m/yy format.
"Darin" <darin_nospam@n ospameverwrote in message
news:u$******** *****@TK2MSFTNG P04.phx.gbl...
Thanks.

I entered in today's date (18/1/2007) and then used:

DateTime.ParseE xact(DRStr(in_o bject), "MM/dd/yyyy",
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo)

and it received an error that the string isn't a valid date (my computer
is set for Spanish(Mexico) so the date is dd/mm/yyyy)

Any ideas.

Darin

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

Jan 18 '07 #6
But CDate won't work because it puts it in the format the computer is
set to and not the format I want it in.

Darin

*** Sent via Developersdex http://www.developersdex.com ***
Jan 18 '07 #7
I guess I don't understand what the parseexact is to do, and the
documentation wasn't too much of a help. When I did the following with
18/1/2007, I got the last message to be 18/1/2007, which makes it look
like the datetime is putting it back to the global settings.

sjunk = DRStr(in_object )
If System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern
<"M/d/yyyy" Then
' not USA, create a string in USA format
Dim xpattern As String
xpattern =
System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.ShortDat ePattern
If Mid(xpattern, 1, 1) = "d" Then 'd/m/y
' day is first
Dim ddate As String
Dim xflds() As String
xflds = Split(DRStr(sju nk), "/")
ddate = xflds(1) & "/" & xflds(0) & "/" & Mid(xflds(2), 1, 4)
'changed to m/d/y
sjunk = ddate
ShowMessage("A-" & sjunk) ' 1/18/2007
End If
End If
Dim format As New System.Globaliz ation.CultureIn fo("en-US", True)

ShowMessage("B-" & sjunk) ' 1/18/2007
ShowMessage("C-" & DRStr(DateTime. ParseExact(sjun k, "M/d/yyyy",
format))) ' 18/1/2007
Return DateTime.ParseE xact(sjunk, "M/d/yyyy", format)

Darin

*** Sent via Developersdex http://www.developersdex.com ***
Jan 18 '07 #8
Darin,

I get more and more the idea that you want your Dates been showed in a way
that only people from the USA understand.

However there is no problem at all to do that.

Public Class Main
Public Shared Sub Main()
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo("en-US")

MessageBox.Show (CDate("01-06-2004 11:59 PM").AddDays(1) .ToString)

'And set the culture back when it was not your own culture
Threading.Threa d.CurrentThread .CurrentCulture = _
Globalization.C ultureInfo.Inst alledUICulture
End Sub
End Class

Cor
Jan 18 '07 #9
Ok. I have a routine that accepts a date argument and needs a date
returned, but in true USA format.

public function USADate(in_date as date) As date
dim xout as date
Threading.Threa d.CurrentThread .CurrentCulture = _
New Globalization.C ultureInfo("en-US")

xout=cdate(in_d ate)

'And set the culture back when it was not your own culture
Threading.Threa d.CurrentThread .CurrentCulture = _
Globalization.C ultureInfo.Inst alledUICulture

return xout
end function

If I change the culture in the routine, and change it back, then return
a date, what date is it going to be, USA format or Mexico? I just need a
routine that can take any date format and return a date in true US
format so it can write it to the database correctly.

Darin

*** Sent via Developersdex http://www.developersdex.com ***
Jan 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
23677
by: Dwalker | last post by:
I just want to enter today's date in a text input box with the format mm/dd/yyyy when it receives focus. I've been playing around with this but just can't seem to get it to work. Any help would be appreciated. Doug
7
32971
by: Marc Pelletier | last post by:
Hello, I have a table with a Day field, defined as smalldatetime. I am filling it from a CSharp application with the following code: DataRow r = dtStaDays.NewRow(); r= station_ID; r = sd.Date; r = rangeTide; etc.
5
11199
by: C. Smith | last post by:
I am a web developer building html data entry forms that have a lot of date input fields (month, date, year) on them. I'd like to have a "popup" calendar that allows the user to pick the date using a graphical calendar interface. For example, the popup would show an entire month of dates and allow the user to scroll to different months/years, and finally double click on a date. The popup then closes and the date is returned (probably as a...
3
1980
by: MattB | last post by:
I've hit a snag with an application I wrote because of the differing date formats in different countries. It's a set of pages that make calls to a COM object that I have wrapped in a web service. The COM object isn't mine, and it wants to receive dates in a short date format, and it seems to inherit the localized settings of it's host machine. The machine with that service/COM object is in Canada and is set to use dd/mm/yyyy date format,...
10
8459
by: okaminer | last post by:
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
1
1840
by: joeyej | last post by:
I'm using this code to make sure users are unable to choose a date from a drop list (<option value="June 8, 2006, (Thursday), 9am">June 12, 2006, (Thursday), 9am) that is less than two days out from the system date. How can I improve it to be sure weekends are excluded? I also need to make sure end of month/begin new month is included in choices being at least 2 days away from system date - . Any help is appreciated. Thanks, Joe
17
5284
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
6
1563
by: vdicarlo | last post by:
I am a programming amateur and a Python newbie who needs to convert about 100,000,000 strings of the form "1999-12-30" into ordinal dates for sorting, comparison, and calculations. Though my script does a ton of heavy calculational lifting (for which numpy and psyco are a blessing) besides converting dates, it still seems to like to linger in the datetime and time libraries. (Maybe there's a hot module in there with a cute little function...
7
2214
by: axapta | last post by:
Hi I need the input of a text field to be as dd/mm/yyyy how can I achieve this? Regards
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.