473,785 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling dates in VB.NET

Yog
Is there a best way to handle various formats of dates for SQL server?.

The data comes in various input files with different date formats.

The ParseExact function looks like an "evil" and it doesn't seem to work for
some cases.
'Date Time Stuff
Dim dtarray As String() = _
{"yyyyMMdd", "yyyy-MM-dd", "yyyy/MM/dd", _
"yyyyMd", "yyyy-M-d", "yyyy/M/d", _
"MMddyyyy", "MM-dd-yyyy", "MM/dd/yyyy", _
"Mdyyyy", "M-d-yyyy", "M/d/yyyy", _
"yyMMdd", "yy-MM-dd", "yy/MM/dd", _
"yyMd", "yy-M-d", "yy/M/d", _
"MMddyy", "MM-dd-yy", "MM/dd/yy", _
"Mdyy", "M-d-yy", "M/d/yy", _
" ", " ", " ", "", " "}

Dim culture = New System.Globaliz ation.CultureIn fo("", True)
Private Function ConvertDataObje ct(ByVal Column As DataColumn, ByVal Obj As
String, ByVal currLineNo As Integer) As String
'This builds the data type from the destination database

If (Obj Is GetType(DBNull) ) Then
Obj = "null"
End If

If (Column.DataTyp e Is GetType(System. String)) Then
'Compensate for aposterphes
Obj = Obj.Replace("'" , "''")

Return "'" & Obj & "'"
ElseIf (Column.DataTyp e Is GetType(System. DateTime)) Then
'Catch faulty date/time data types
Try

If Obj = " " Or Obj = "" Or Obj = " " Then
Obj = ""
Else
Obj = DateTime.ParseE xact(Obj, dtarray, culture,
Globalization.D ateTimeStyles.N one)
End If
Catch ex As Exception
If Not (fileLogger Is Nothing) Then
'HasGlobalError = True
fileLogger.LogD irectEntry("War ning: An invalid date/time
field of " & Obj & " was identified on line #" & currLineNo.ToSt ring() & ",
column " & Column.ColumnNa me & ", position " & Column.Ordinal. ToString() & ".
The field was truncated to a null entry to allow the record to be processed.")
End If
Return "null"
End Try

Return "'" & Obj & "'"
Else
Return Obj

End If
Nov 22 '05 #1
2 4515
Yog, one solution for this is while returning from database itself format the
date in particular format (ddmmyyyy) and return to UI.

"Yog" wrote:
Is there a best way to handle various formats of dates for SQL server?.

The data comes in various input files with different date formats.

The ParseExact function looks like an "evil" and it doesn't seem to work for
some cases.
'Date Time Stuff
Dim dtarray As String() = _
{"yyyyMMdd", "yyyy-MM-dd", "yyyy/MM/dd", _
"yyyyMd", "yyyy-M-d", "yyyy/M/d", _
"MMddyyyy", "MM-dd-yyyy", "MM/dd/yyyy", _
"Mdyyyy", "M-d-yyyy", "M/d/yyyy", _
"yyMMdd", "yy-MM-dd", "yy/MM/dd", _
"yyMd", "yy-M-d", "yy/M/d", _
"MMddyy", "MM-dd-yy", "MM/dd/yy", _
"Mdyy", "M-d-yy", "M/d/yy", _
" ", " ", " ", "", " "}

Dim culture = New System.Globaliz ation.CultureIn fo("", True)
Private Function ConvertDataObje ct(ByVal Column As DataColumn, ByVal Obj As
String, ByVal currLineNo As Integer) As String
'This builds the data type from the destination database

If (Obj Is GetType(DBNull) ) Then
Obj = "null"
End If

If (Column.DataTyp e Is GetType(System. String)) Then
'Compensate for aposterphes
Obj = Obj.Replace("'" , "''")

Return "'" & Obj & "'"
ElseIf (Column.DataTyp e Is GetType(System. DateTime)) Then
'Catch faulty date/time data types
Try

If Obj = " " Or Obj = "" Or Obj = " " Then
Obj = ""
Else
Obj = DateTime.ParseE xact(Obj, dtarray, culture,
Globalization.D ateTimeStyles.N one)
End If
Catch ex As Exception
If Not (fileLogger Is Nothing) Then
'HasGlobalError = True
fileLogger.LogD irectEntry("War ning: An invalid date/time
field of " & Obj & " was identified on line #" & currLineNo.ToSt ring() & ",
column " & Column.ColumnNa me & ", position " & Column.Ordinal. ToString() & ".
The field was truncated to a null entry to allow the record to be processed.")
End If
Return "null"
End Try

Return "'" & Obj & "'"
Else
Return Obj

End If

Nov 22 '05 #2
Yog
Thanks for answering...i am looking at dates that comes in various files with
different formats...not from the database..

"Santhi Maadhaven" wrote:
Yog, one solution for this is while returning from database itself format the
date in particular format (ddmmyyyy) and return to UI.

"Yog" wrote:
Is there a best way to handle various formats of dates for SQL server?.

The data comes in various input files with different date formats.

The ParseExact function looks like an "evil" and it doesn't seem to work for
some cases.
'Date Time Stuff
Dim dtarray As String() = _
{"yyyyMMdd", "yyyy-MM-dd", "yyyy/MM/dd", _
"yyyyMd", "yyyy-M-d", "yyyy/M/d", _
"MMddyyyy", "MM-dd-yyyy", "MM/dd/yyyy", _
"Mdyyyy", "M-d-yyyy", "M/d/yyyy", _
"yyMMdd", "yy-MM-dd", "yy/MM/dd", _
"yyMd", "yy-M-d", "yy/M/d", _
"MMddyy", "MM-dd-yy", "MM/dd/yy", _
"Mdyy", "M-d-yy", "M/d/yy", _
" ", " ", " ", "", " "}

Dim culture = New System.Globaliz ation.CultureIn fo("", True)
Private Function ConvertDataObje ct(ByVal Column As DataColumn, ByVal Obj As
String, ByVal currLineNo As Integer) As String
'This builds the data type from the destination database

If (Obj Is GetType(DBNull) ) Then
Obj = "null"
End If

If (Column.DataTyp e Is GetType(System. String)) Then
'Compensate for aposterphes
Obj = Obj.Replace("'" , "''")

Return "'" & Obj & "'"
ElseIf (Column.DataTyp e Is GetType(System. DateTime)) Then
'Catch faulty date/time data types
Try

If Obj = " " Or Obj = "" Or Obj = " " Then
Obj = ""
Else
Obj = DateTime.ParseE xact(Obj, dtarray, culture,
Globalization.D ateTimeStyles.N one)
End If
Catch ex As Exception
If Not (fileLogger Is Nothing) Then
'HasGlobalError = True
fileLogger.LogD irectEntry("War ning: An invalid date/time
field of " & Obj & " was identified on line #" & currLineNo.ToSt ring() & ",
column " & Column.ColumnNa me & ", position " & Column.Ordinal. ToString() & ".
The field was truncated to a null entry to allow the record to be processed.")
End If
Return "null"
End Try

Return "'" & Obj & "'"
Else
Return Obj

End If

Nov 22 '05 #3

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

Similar topics

1
1515
by: rob | last post by:
Hey, I've got an Classic ASP app pulling some records out of a DB, which worked fine when it was Access. When we upped to SQL Server, all of a sudden, when the Recordsets came back, all the dates were empty, even though the data was showing up in the DB. About a year ago (only code asp apps once and a while), I remember seeing this and finding something that just had something to do with the order in which something was being...
3
1537
by: Sune | last post by:
I'm working on a system that requires alot of date handling, and i'm getting pretty tired of doing custom calculating when modifying dates, so i hope someone would be able to help me. Is there any easy way to do re-calculating of dates? such as adding 2,11 or 36 months to a date, and have it automatically checking whether or not it should increase months or year, and by how much? Like, if i have to add 11 months to the date right now, i...
2
1200
by: Child | last post by:
I am trying to insert some data into a table and keep gettting the error: SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM. Its me testing the database so the dates should be dates. In the CodeBehind page of the form that calls this AddEvent function I have tried converting the txtStartDate.Text to a CDate to pass to this function, but same error. Is there something obvious i am missing?
6
1963
by: Trekmp | last post by:
I currently work in ASP and am in the process of moving some application across to PHP. At present I store dates in a database in numerical format 38694, which is 08/12/2005. In ASP its very easy to convert this value back into a date, but I cannot find a way to do this in PHP. Thanks for any help
3
1392
by: Dr John Stockton | last post by:
For an all-numeric Gregorian date to be intrinsically unambiguous, it must generally be of the form /\d{3,}\D+\d+\D+\d+/ ; and we've discussed handling that. But if the month is given in letters (in full, as a LTA, or as Roman numerals) , then the date is unambiguous if the value of the year field is greater than the greatest possible day field value - in which case the year value is greater than the actual day value - whatever...
5
2639
by: AAJ | last post by:
Hi Does anyone know of any good publically available set of standards for managing dates when dealing with a database server (in my case SQL Server 2000 and c# VS2005). At the moment, if I create a record in the database for say a job, the invoicedOn date field will be left null within the data base, not being assigned untill its actually invoiced.
5
1332
by: Smokey Grindle | last post by:
Ok I must admit I stink at regular expressions... been trying to learn them for a while now and its not sticking how I wish it would... but I am trying to take a very long string (about 30KB) and pull out all the dates in it that are in mm/dd/yyyy format and put them into a collection... how would you go about writing this regex? and then how would you move them into a collection? thanks a lot!
4
3327
by: Jeff Goodman | last post by:
If there is a better newsgroup to post this in, please let me know. I am a relatively new VB.NET/SQL 2000 programmer. I am working with data imported into SQL2K from Access. Many of the dates are null, such as the terminate date with current employees. I have written the search function to find a certain employee, and now I am editing the selected records. Several of the fields are date fields, and I am trying to format them so...
2
2197
by: Jim Carlock | last post by:
(1) Does PHP provide any way to handle dates prior to 1980? I know there's problems with Microsoft Windows NT and all Windows NT operating systems will allow a date prior to 1980 to be placed upon a file, but it ends up not being able to display the date (probably an Windows Explorer specific issue). (2) I'm looking for for ways to store dates and process dates (dates only). This probably ends up as a database specific issue, so I'm...
0
9645
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
9480
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
9950
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
7499
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
6740
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();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.