473,770 Members | 1,645 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
21 3387
Take a step back for a moment.

Create a new Windows Forms project, place a button (Button1) on the form and
paste the following code into the
form.

Click the button and observe the results.

Change the initial date string and the 'foreign' culture to different values
and observe the results.

Once you have got a feel for what it is doing, then you can incorporate it
into your application.

Private Sub Button1_Click(B yVal sender As Object, ByVal e As EventArgs)
Handles Button1.Click

Try
Console.WriteLi ne(GetUSDateStr ing("19/1/2007"))
Catch _ex As Exception
Console.WriteLi ne(_ex.ToString )
End Try

End Sub

Private Function GetUSDateString (ByVal value As String) As String

' The following is commented out so we can simulate different cultures
'Dim myCI As CultureInfo = New
CultureInfo(Cul tureInfo.Curren tCulture.Name, False)

' The following line is used to simulate different cultures
' Change the en-NZ to es-MX or the name for any other culture you desire
Dim _myCI As CultureInfo = New CultureInfo("en-NZ", False)

' CultureInfo object for en-US culture
Dim _usCI As CultureInfo = New CultureInfo("en-US", False)

Dim _date As DateTime

Try
' Attempt to parse value using the 'foreign' culture
_date = DateTime.Parse( value, _myCI.DateTimeF ormat)
Catch _ex As FormatException
' That didn't work
Try
' so Attempt to parse value using the en_US culture
_date = DateTime.Parse( value, _usCI.DateTimeF ormat)
Catch _exx As FormatException
' That didn't work either
Try

' so Attempt to parse value using the en_US culture again after
swapping the first 2 parts of value about
_date = DateTime.Parse( Swap(value,
Convert.ToChar( _usCI.DateTimeF ormat.DateSepar ator)), _usCI.DateTimeF ormat)
Catch _exxx As Exception
' That still didn;t work so throw the exception up the line
Throw
End Try
Catch _exx As Exception
' Some other exception happened so throw ot up the line
Throw
End Try
Catch _ex As Exception

' Some other exception happened so throw ot up the line
Throw
End Try

' One of the parse attempts succeeded so return the result in the
ShortDatePatter n of the en_US culture
Return _date.ToString( "d", _usCI.DateTimeF ormat)

End Function

Private Function Swap(ByVal value As String, ByVal delimiter As Char) As
String

Dim _ss As String() = value.Split(del imiter)

Dim _s As String = _ss(0)

_ss(0) = _ss(1)

_ss(1) = _s

Return String.Join(del imiter, _ss)

End Function
"Darin" <darin_nospam@n ospameverwrote in message
news:uK******** ******@TK2MSFTN GP04.phx.gbl...
>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 19 '07 #11
Darin,

Your database is not set in USA format, it is in ticks from 1000/3
milliseconds starting at januari 1 1873 (for SQL server). It is in every
culture the same.

Cor
Jan 19 '07 #12
Hello Darin,
Ok. I have a routine that accepts a date argument and needs a date
returned, but in true USA format.
I understand that you want to get a date as a string like in traditional
BASIC?

Private Function BASICdate(byval ThisDate As Date) As String
Return (ThisDate.Month & "/" & ThisDate.Day & "/" & ThisDate.Year
End Function

Best regards,

Martin
Jan 21 '07 #13
Sorry, routine was not complete, now it is:

Private Function BASICdate(byval ThisDate As Date) As String
Return Format(ThisDate .Month,"00") & "/" & _
Format(ThisDate .Day,"00") & "/" & _
Cstr(ThisDate.Y ear)
End Function

Best regards,

Martin

Jan 21 '07 #14
"Martin H." <hk***@gmx.nets chrieb
Hello Darin,
Ok. I have a routine that accepts a date argument and needs a date
returned, but in true USA format.

I understand that you want to get a date as a string like in
traditional BASIC?

Private Function BASICdate(byval ThisDate As Date) As String
Return (ThisDate.Month & "/" & ThisDate.Day & "/" & ThisDate.Year
End Function
Why not use ThisDate.ToStri ng instead of writing another function? The
format can be a constant.
Armin

Jan 21 '07 #15
Martin,
As Armin suggests: You can simplify that to:
Private Function BASICdate(byval ThisDate As Date) As String
Return ThisDate.ToStri ng("MM/dd/yyyy")
End Function
Which also ensures that the year will be 4 digits.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"HKSHK" <hk***@gmx.netw rote in message
news:11******** **************@ l53g2000cwa.goo glegroups.com.. .
Sorry, routine was not complete, now it is:

Private Function BASICdate(byval ThisDate As Date) As String
Return Format(ThisDate .Month,"00") & "/" & _
Format(ThisDate .Day,"00") & "/" & _
Cstr(ThisDate.Y ear)
End Function

Best regards,

Martin
Jan 21 '07 #16

Darin wrote:
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?
<snip>

Neither. A variable of type Date or DateTime is in a "neutral", binary
format, that has nothing to do with the thread's culture (and I guess
that's what Cor is trying to inform you).

It seems you're mistaking a date value with the date representation.
The date value is binary and, again, has nothing to do with a given
culture. The date representation, on the other side, is a string, and
varies according to the culture being used by the thread, the system's
date format, etc.

HTH.

Regards,

Branco.

Jan 22 '07 #17
Hello Jay,
As Armin suggests: You can simplify that to:
>Private Function BASICdate(byval ThisDate As Date) As String
Return ThisDate.ToStri ng("MM/dd/yyyy")
End Function
No, the simplification won't work, because the OP wrote that he wants to
have the US date. The US date has slashes in it (e.g. 01/31/2007).

However, the format command will replace the slashes with the separators
for your country. In my case (Germany) we use dots. With the simplified
routine I get 01.31.2007. To get the real BASIC date the best option is
to assemble the string as I did before.

Best regards,

Martin
Jan 22 '07 #18
Hello Branco,
Neither. A variable of type Date or DateTime is in a "neutral", binary
format, that has nothing to do with the thread's culture (and I guess
that's what Cor is trying to inform you).

It seems you're mistaking a date value with the date representation.
The date value is binary and, again, has nothing to do with a given
culture. The date representation, on the other side, is a string, and
varies according to the culture being used by the thread, the system's
date format, etc.
Correct and incorrect. You are correct that a date variable contains a
"neutral" binary format. However, you don't know what the OP wants to
accomplish. Perhaps he wants to use the date in an SQL query. In this
case he would have to use a string in US format.

Best regards,

Martin
Jan 22 '07 #19

Martin H. wrote:
<snip>
Correct and incorrect. You are correct that a date variable contains a
"neutral" binary format. However, you don't know what the OP wants to
accomplish. Perhaps he wants to use the date in an SQL query. In this
case he would have to use a string in US format.
<snip>

Notice that I didn't mention anything related to formatting the date, I
suppose others had already given that information. Therefore I don't
see the "incorrect" part of my post.

Regards,

Branco.

Jan 22 '07 #20

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
8461
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
9595
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
9432
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
10232
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
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9873
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...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
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.