473,383 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.Globalization.CultureInfo("", True)
Private Function ConvertDataObject(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.DataType Is GetType(System.String)) Then
'Compensate for aposterphes
Obj = Obj.Replace("'", "''")

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

If Obj = " " Or Obj = "" Or Obj = " " Then
Obj = ""
Else
Obj = DateTime.ParseExact(Obj, dtarray, culture,
Globalization.DateTimeStyles.None)
End If
Catch ex As Exception
If Not (fileLogger Is Nothing) Then
'HasGlobalError = True
fileLogger.LogDirectEntry("Warning: An invalid date/time
field of " & Obj & " was identified on line #" & currLineNo.ToString() & ",
column " & Column.ColumnName & ", 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 4496
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.Globalization.CultureInfo("", True)
Private Function ConvertDataObject(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.DataType Is GetType(System.String)) Then
'Compensate for aposterphes
Obj = Obj.Replace("'", "''")

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

If Obj = " " Or Obj = "" Or Obj = " " Then
Obj = ""
Else
Obj = DateTime.ParseExact(Obj, dtarray, culture,
Globalization.DateTimeStyles.None)
End If
Catch ex As Exception
If Not (fileLogger Is Nothing) Then
'HasGlobalError = True
fileLogger.LogDirectEntry("Warning: An invalid date/time
field of " & Obj & " was identified on line #" & currLineNo.ToString() & ",
column " & Column.ColumnName & ", 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.Globalization.CultureInfo("", True)
Private Function ConvertDataObject(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.DataType Is GetType(System.String)) Then
'Compensate for aposterphes
Obj = Obj.Replace("'", "''")

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

If Obj = " " Or Obj = "" Or Obj = " " Then
Obj = ""
Else
Obj = DateTime.ParseExact(Obj, dtarray, culture,
Globalization.DateTimeStyles.None)
End If
Catch ex As Exception
If Not (fileLogger Is Nothing) Then
'HasGlobalError = True
fileLogger.LogDirectEntry("Warning: An invalid date/time
field of " & Obj & " was identified on line #" & currLineNo.ToString() & ",
column " & Column.ColumnName & ", 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
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...
3
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...
2
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...
6
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...
3
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...
5
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...
5
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...
4
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...
2
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.