Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys

I am currently having some trouble converting dates to a standard date

I have built the method and all works well on my dev machine which is Windows 7
when i deploy my service to the UAT(Windows server 2012) i am getting an error System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

not too sure what i am doing wrong

What I have tried:

Public Function ConvertDateToStandard(ByVal Clientid As String, ByVal importtype As String, ByVal ImportDate As String) As String
        Try

        
        Dim ReturnDate As String = ""
        Dim DateFormat As String = ""
            Dim DateFormatSplit() As String
            Dim DateSplit() As String
            Dim CalenderDate() As String
            Dim Timesplit() As String
            Dim year As Integer = "1900"
            Dim Month As Integer = "01"
            Dim Day As Integer = "01"
            Dim Hour As Integer = 0
            Dim Minute As Integer = 0
            Dim second As Integer = 0
            Dim DatePosition As String = ""
            Dim ConversionDate As New DateTime
            Dim calendarFormat() As String
            Dim timeformatstring() As String
            'ImportDate = "Sun 29/01"
        DateSplit = ImportDate.Split(" ".ToCharArray)
        DateFormat = RRSGeneral.GetClientImportDateFormat(Clientid, importtype)

        If DateFormat = "" Then
            If DateSplit(0).Length = 4 Then
                DateFormat = "yyyy/MM/dd HH:mm:ss"
            Else
                DateFormat = "dd/MM/yyyy HH:mm:ss"
            End If

        End If

            If DateFormat.ToUpper.Contains("DDD") Then
                DateFormatSplit = DateFormat.Split(" ".ToCharArray)
                DateSplit = ImportDate.Split(" ".ToCharArray)
                calendarFormat = DateFormatSplit(1).Split("/".ToCharArray)
                CalenderDate = DateSplit(1).Split("/".ToCharArray)
                For count = 0 To calendarFormat.Count - 1
                    If calendarFormat(count).ToString.ToUpper.Contains("Y") Then
                        year = CalenderDate(count)
                    ElseIf calendarFormat(count).ToString.ToUpper.Contains("M") Then
                        Month = CalenderDate(count)
                    ElseIf calendarFormat(count).ToString.ToUpper.Contains("D") Then
                        Day = CalenderDate(count)
                    End If
                Next

                year = Date.Now.Year.ToString

                ConversionDate = New Date(CInt(year), CInt(Month), CInt(Day))
                Dim conversionday As String = ConversionDate.ToString("ddd")

                If conversionday.ToString.ToUpper = DateSplit(0).ToString.ToUpper Then

                Else
                    year = year - 1
                    ConversionDate = New Date(CInt(year), CInt(Month), CInt(Day))
                    If conversionday.ToString.ToUpper = DateSplit(0).ToString.ToUpper Then
                    Else
                        year = year - 1
                        ConversionDate = New Date(CInt(year), CInt(Month), CInt(Day))
                    End If
                End If

            Else
                DateFormatSplit = DateFormat.Split(" ".ToCharArray)
                If DateFormatSplit.Length > 1 Then
                    If ImportDate.ToUpper.Contains(Environment.NewLine) Then
                        ImportDate = ImportDate.Replace(vbCrLf, " ")
                    End If
                    DateSplit = ImportDate.Split(" ".ToCharArray)


                    If DateFormatSplit(0).ToString.ToUpper.Contains("H") Then
                        calendarFormat = DateFormatSplit(1).Split("/".ToCharArray)
                        CalenderDate = DateSplit(1).Split("/".ToCharArray)
                        Timesplit = DateSplit(0).Split(":".ToCharArray)

                        For count = 0 To calendarFormat.Count - 1
                            If calendarFormat(count).ToString.ToUpper.Contains("Y") Then
                                year = CalenderDate(count)
                            ElseIf calendarFormat(count).ToString.ToUpper.Contains("M") Then
                                Month = CalenderDate(count)
                            ElseIf calendarFormat(count).ToString.ToUpper.Contains("D") Then
                                Day = CalenderDate(count)
                            End If
                        Next

                        timeformatstring = DateFormatSplit(0).Split(":".ToCharArray)
                        For count = 0 To timeformatstring.Count - 1
                            If timeformatstring(count).ToString.ToUpper.Contains("H") Then
                                Hour = Timesplit(count)
                            ElseIf timeformatstring(count).ToString.ToUpper.Contains("M") Then
                                Minute = Timesplit(count)
                            ElseIf timeformatstring(count).ToString.ToUpper.Contains("S") Then
                                second = Timesplit(count)
                            End If
                        Next

                        ReturnDate = New Date(CInt(year), CInt(Month), CInt(Day), CInt(Hour), CInt(Minute), CInt(second)).ToString("dd MMM yyyy HH:mm:ss")

                    Else
                        calendarFormat = DateFormatSplit(0).Split("/".ToCharArray)
                        CalenderDate = DateSplit(0).Split("/".ToCharArray)
                        Timesplit = DateSplit(1).Split(":".ToCharArray)

                        For count = 0 To calendarFormat.Count - 1
                            If calendarFormat(count).ToString.ToUpper.Contains("Y") Then
                                year = CalenderDate(count)
                            ElseIf calendarFormat(count).ToString.ToUpper.Contains("M") Then
                                Month = CalenderDate(count)
                            ElseIf calendarFormat(count).ToString.ToUpper.Contains("D") Then
                                Day = CalenderDate(count)
                            End If
                        Next

                        timeformatstring = DateFormatSplit(1).Split(":".ToCharArray)
                        For count = 0 To timeformatstring.Count - 1
                            If timeformatstring(count).ToString.ToUpper.Contains("H") Then
                                Hour = Timesplit(count)
                            ElseIf timeformatstring(count).ToString.ToUpper.Contains("M") Then
                                Minute = Timesplit(count)
                            ElseIf timeformatstring(count).ToString.ToUpper.Contains("S") Then
                                second = Timesplit(count)
                            End If
                        Next

                        ReturnDate = New Date(CInt(year), CInt(Month), CInt(Day), CInt(Hour), CInt(Minute), CInt(second)).ToString("dd MMM yyyy HH:mm:ss")
                    End If
                Else
                    calendarFormat = DateFormatSplit(0).Split("/".ToCharArray)
                    CalenderDate = DateSplit(0).Split("/".ToCharArray)
                    'Timesplit = DateSplit(1).Split(":".ToCharArray)

                    For count = 0 To calendarFormat.Count - 1
                        If calendarFormat(count).ToString.ToUpper.Contains("Y") Then
                            year = CalenderDate(count)
                        ElseIf calendarFormat(count).ToString.ToUpper.Contains("M") Then
                            Month = CalenderDate(count)
                        ElseIf calendarFormat(count).ToString.ToUpper.Contains("D") Then
                            Day = CalenderDate(count)
                        End If


                    Next
                    ReturnDate = New Date(CInt(year), CInt(Month), CInt(Day), CInt(Hour), CInt(Minute), CInt(second)).ToString("dd MMM yyyy HH:mm:ss")


                End If
            End If

            'MyReturnDate = New Date(CInt(year), CInt(Month), CInt(Day), CInt(Hour), CInt(Min), CInt(Seconds))

            'Return MyReturnDate.ToString("dd MMM yyyy HH:mm:ss")

            'ReturnDate = Convert.ToDateTime(ConversionDate.ToString).ToString("dd MMM yyyy HH:mm:ss")
                Return ReturnDate
Posted
Updated 31-Jan-17 6:18am
Comments
IsiR_19 1-Feb-17 8:42am    
Found the issue file was updating during an excel conversion to CSV and date was using the culture date

We can't tell you because we can't run your code with exactly the same data you are feeding it - and it's the data which decides which path it takes through the code and thus what exactly the code is doing.
So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
your logic looks more complicated to me. Better & safer bet is to use system culture & apply data formater on top of culture and date

Both culture & formater logic are available here Custom Date and Time Format Strings[^]
 
Share this answer
 
Quote:
System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.

The problem is data dependent, we can't know the reason without the data.
I see 2 possibilities to help understand the problem:
1) use a debugger to see the data at faillure.
2) log the data on each call in a file, after crash, the last data is the offending one.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900