Skip to content

Instantly share code, notes, and snippets.

@martinbowling
Created January 27, 2021 21:55
Show Gist options
  • Save martinbowling/d45358380668c4aa031c2ab84208b858 to your computer and use it in GitHub Desktop.
Save martinbowling/d45358380668c4aa031c2ab84208b858 to your computer and use it in GitHub Desktop.
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Namespace AjaxDataScrubber
Class MainClass
Public Shared months As Dictionary(Of String, String) = New Dictionary(Of String, String)() From {
{"january", "01"},
{"jan", "01"},
{"february", "02"},
{"feb", "02"},
{"march", "03"},
{"mar", "03"},
{"april", "04"},
{"apr", "04"},
{"may", "05"},
{"june", "06"},
{"july", "07"},
{"august", "08"},
{"aug", "08"},
{"september", "09"},
{"sept", "09"},
{"october", "10"},
{"oct", "10"},
{"november", "11"},
{"nov", "11"},
{"december", "12"},
{"dec", "12"}
}
Public Shared Sub Main(ByVal args As String())
Dim filePath = "data.csv"
Dim data = File.ReadLines(filePath)
For Each d In data
Console.WriteLine(d.ToString() & " --> " & fixDate(d).ToShortDateString())
Next
Console.ReadLine()
End Sub
Public Shared Function fixDate(ByVal dateString As String) As DateTime
Dim date As DateTime
Dim parsed = DateTime.TryParse(dateString, date)
If parsed Then
Return date
Else
dateString = fixMonth(dateString)
dateString = dateString.ToLower().Replace("st", "").Replace("th", "").Replace("rd", "")
parsed = DateTime.TryParse(dateString, date)
If parsed Then
Return date
Else
Return DateTime.Now
End If
End If
End Function
Public Shared Function fixMonth(ByVal fixMonth As String) As String
For Each month In months
If fixMonth.ToLower().Contains(month.Key) Then
fixMonth = fixMonth.Replace(month.Key, month.Value)
Exit For
End If
Next
Return fixMonth
End Function
End Class
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment