Get date to specific formate

I want to convert "current" ("10/27/2015 10:28:07 AM") to  string formate like "2015-10-27 00:00:00". I am having problem with a line below.

Public Shared Function AddBusinessDays_list(current As DateTime, days As Integer) As List(Of String)

    Dim result As New List(Of String)
    Dim sign = Math.Sign(days)
    Dim unsignedDays = Math.Abs(days)
    For i As Object = 0 To unsignedDays - 1
        Do
            current = current.AddDays(sign)
            If current.DayOfWeek <> DayOfWeek.Saturday And current.DayOfWeek <> DayOfWeek.Sunday Then
            '10/27/2015 10:28:07 AM
            Dim dt As Date = Date.ParseExact(current, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture)  <<<<< Error this line 
            result.Add(date.ToString)
            End If

        Loop While current.DayOfWeek = DayOfWeek.Saturday OrElse current.DayOfWeek = DayOfWeek.Sunday
    Next
    Return result
End Function

Open in new window

VBdotnet2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Your "current" variable is already a DateTime, so you don't need to parse anything.

Just call ToString() on it:
Public Shared Function AddBusinessDays_list(current As DateTime, days As Integer) As List(Of String)

    Dim result As New List(Of String)
    Dim sign = Math.Sign(days)
    Dim unsignedDays = Math.Abs(days)
    For i As Object = 0 To unsignedDays - 1
        Do
            current = current.AddDays(sign)
            If current.DayOfWeek <> DayOfWeek.Saturday And current.DayOfWeek <> DayOfWeek.Sunday Then
            ' 10/27/2015 10:28:07 AM --> 2015-10-27 00:00:00
            result.Add(current.ToString("yyyy-MM-dd hh:mm:ss"))
            End If
        Loop While current.DayOfWeek = DayOfWeek.Saturday OrElse current.DayOfWeek = DayOfWeek.Sunday
    Next
    Return result
End Function

Open in new window

1
anarki_jimbelSenior DeveloperCommented:
Just to add: below are two good links about string formatting, for dates, numbers, etc.

http://blogs.msdn.com/b/kathykam/archive/2006/03/29/564426.aspx

http://blog.stevex.net/string-formatting-in-csharp/
0
ArkCommented:
First of all, your function is incorrect - you loop twice for each day - first in for-next, then in do-loop. Try
    Public Shared Function AddBusinessDays_list(current As DateTime, days As Integer) As List(Of String)
        Dim newDate = current.AddDays(days)
        Dim dtStart = If(current < newDate, current, newDate)
        Dim dtEnd = If(dtStart = current, newDate, current)
        Dim lst As New List(Of String)
        Do
            If Not IsHoliday(dtStart) Then
                lst.Add(dtStart.ToString("yyyy-MM-dd"))
            End If
            dtStart = dtStart.AddDays(1)
        Loop While dtStart <= dtEnd
        Return lst
    End Function
    Private Shared Function IsHoliday(dt As Date) As Boolean
        Return dt.DayOfWeek = DayOfWeek.Saturday OrElse dt.DayOfWeek = DayOfWeek.Sunday
    End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ArkCommented:
A bit improved version (taking official holidays into account)
    'Holidays in Russia in dd.MM format
    Private Shared _holidays As String() = "01.01,02.01,03.01,04.01,05.01,06.01,07.01,08.01,23.02,08.03,01.05,09.05,12.06,04.11".Split(","c)

    Public Shared Function AddBusinessDays_list(current As DateTime, days As Integer) As List(Of String)
        Dim newDate = current.AddDays(days)
        Dim dtStart = If(current < newDate, current, newDate)
        Dim dtEnd = If(dtStart = current, newDate, current)
        Dim lst As New List(Of String)
        Do
            If Not IsHoliday(dtStart) Then
                lst.Add(dtStart.ToString("yyyy-MM-dd"))
            End If
            dtStart = dtStart.AddDays(1)
        Loop While dtStart <= dtEnd
        Return lst
    End Function

    Private Shared Function IsHoliday(dt As Date) As Boolean
        Dim s = dt.ToString("dd.MM")
        Return dt.DayOfWeek = DayOfWeek.Saturday OrElse dt.DayOfWeek = DayOfWeek.Sunday OrElse _holidays.Contains(s)
    End Function

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.