如何将日期转换为标准 [英] How do I convert date to a standard

查看:72
本文介绍了如何将日期转换为标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好



我目前在将日期转换为标准日期时遇到一些麻烦



我已经构建了这个方法,并且在我的开发机器上运行良好,这是Windows 7

,当我将服务部署到UAT(Windows服务器2012)时,我收到错误System.ArgumentOutOfRangeException:Year,Month,和Day参数描述了一个无法表示的DateTime。



不太确定我做错了什么



我尝试过:



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

推荐答案

我们可以不要告诉你,因为我们无法使用与你提供的数据完全相同的数据来运行你的代码 - 而且数据决定了代码所采用的路径,以及代码究竟在做什么。

所以,它取决于你。

在函数的第一行放置断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
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!


你的逻辑看起来对我来说更复杂。更好的&更安全的赌注是使用系统文化&在文化和日期之上应用数据格式化器



文化和文化此处提供了格式化逻辑自定义日期和时间格式字符串 [< a href =https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]
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[^]


Quote:

System.ArgumentOutOfRangeException:Year,Month和Day参数描述了一个不可表示的DateTime。

System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.



问题是数据依赖的,我们无法知道没有数据的原因。
我看到2种可能性来帮助理解问题:

1)使用调试器查看faillure上的数据。

2)记录文件中每次调用的数据,崩溃后,最后一个数据是有问题的。



如果你不明白你的代码在做什么或为什么它做了什么,答案是调试器

使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


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.


这篇关于如何将日期转换为标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆