C# Snippets


Date_Format

public static string formatDate(string dateString)
{
try
{
string formattedDateString = null;
System.DateTime date;

if (System.String.IsNullOrEmpty(dateString))
return formattedDateString;

date = System.DateTime.Parse(dateString);
formattedDateString = date.ToString(Constants.COVER_DATE_FORMAT);

return formattedDateString;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return "";
}
}

Date_FormatUSUK

Converts a date string between the US and the UK formats.
public static string formatDate(string dateString)
{
try
{
string formattedDateString = null;
System.DateTime date;

if (System.String.IsNullOrEmpty(dateString))
return formattedDateString;

date = System.DateTime.Parse(dateString);
formattedDateString = date.ToString(Constants.COVER_DATE_FORMAT);

return formattedDateString;
}
catch (System.Exception ex)
{
ExceptionHandling.MessageShow(System.Reflection.MethodBase.GetCurrentMethod(), ex);
return "";
}
}

DateTime_Current

Public Function DateTime_Current(ByVal sDateTimeFormat As String) As String
Try
If My.Settings.USER_ERROR_OCCURRED = True Then Return "" : Exit Function

Return System.DateTime.Now.ToString(sDateTimeFormat)

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return ""
End Try
End Function

DateTime_DateFormat

Public Shared Function DateFormat(ByVal iDayValue As System.Int32, _
ByVal sDayFormat As System.String, _
ByVal iMonthValue As System.Int32, _
ByVal sMonthFormat As System.String, _
ByVal iYearValue As System.Int32, _
ByVal sYearFormat As System.String, _
ByVal sSeperatorChar As System.String) _
As String

Try
If gbEND_GENERAL = True Then Exit Function

Dim syear As System.String
Dim sdateformat As System.String

Select Case sDayFormat
Case "d", "D"
sdateformat = sdateformat & iDayValue

Case "dd", "DD"
If iDayValue <= 9 Then sdateformat = sdateformat & "0" & iDayValue
If iDayValue > 9 Then sdateformat = sdateformat & iDayValue
End Select

sdateformat = sdateformat & sSeperatorChar

Select Case sMonthFormat

Case "mm"
If iMonthValue <= 9 Then sdateformat = sdateformat & "0" & iMonthValue
If iMonthValue > 9 Then sdateformat = sdateformat & iMonthValue

Case "mmm"
Select Case iMonthValue
Case 1 : sdateformat = sdateformat & "jan"
Case 2 : sdateformat = sdateformat & "feb"
Case 3 : sdateformat = sdateformat & "mar"
Case 4 : sdateformat = sdateformat & "apr"
Case 5 : sdateformat = sdateformat & "may"
Case 6 : sdateformat = sdateformat & "jun"
Case 7 : sdateformat = sdateformat & "jul"
Case 8 : sdateformat = sdateformat & "aug"
Case 9 : sdateformat = sdateformat & "sep"
Case 10 : sdateformat = sdateformat & "oct"
Case 11 : sdateformat = sdateformat & "nov"
Case 12 : sdateformat = sdateformat & "dec"
End Select

Case "mmmm"
Select Case iMonthValue
Case 1 : sdateformat = sdateformat & "january"
Case 2 : sdateformat = sdateformat & "february"
Case 3 : sdateformat = sdateformat & "march"
Case 4 : sdateformat = sdateformat & "april"
Case 5 : sdateformat = sdateformat & "may"
Case 6 : sdateformat = sdateformat & "june"
Case 7 : sdateformat = sdateformat & "july"
Case 8 : sdateformat = sdateformat & "august"
Case 9 : sdateformat = sdateformat & "september"
Case 10 : sdateformat = sdateformat & "october"
Case 11 : sdateformat = sdateformat & "november"
Case 12 : sdateformat = sdateformat & "december"
End Select
End Select

sdateformat = sdateformat & sSeperatorChar

syear = iYearValue.ToString
Select Case sYearFormat
Case "yy"
sdateformat = sdateformat & syear.Substring(syear.Length - 2, 2)

Case "yyyy"
sdateformat = sdateformat & iYearValue

End Select

DateFormat = sdateformat

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("DateFormat", msCLASSNAME, _
"", _
mobjCOMException, mobjException)
End If
End Try
End Function

DateTime_MonthIntegerReturn

Public Shared Function MonthIntegerReturn(ByVal sMonthText As System.String, _
ByVal sMonthFormat As System.String) _
As System.Int32

Dim ireturn As System.Int32

Try
If gbEND_GENERAL = True Then Exit Function

Select Case sMonthFormat

Case "mmm"
Select Case sMonthText.ToLower
Case "jan" : ireturn = 1
Case "feb" : ireturn = 2
Case "mar" : ireturn = 3
Case "apr" : ireturn = 4
Case "may" : ireturn = 5
Case "jun" : ireturn = 6
Case "jul" : ireturn = 7
Case "aug" : ireturn = 8
Case "sep" : ireturn = 9
Case "oct" : ireturn = 10
Case "nov" : ireturn = 11
Case "dec" : ireturn = 12
End Select

Case "mmmm"
Select Case sMonthText.ToLower
Case "january" : ireturn = 1
Case "february" : ireturn = 2
Case "march" : ireturn = 3
Case "april" : ireturn = 4
Case "may" : ireturn = 5
Case "june" : ireturn = 6
Case "july" : ireturn = 7
Case "august" : ireturn = 8
Case "september" : ireturn = 9
Case "october" : ireturn = 10
Case "november" : ireturn = 11
Case "december" : ireturn = 12
End Select
End Select

MonthIntegerReturn = ireturn

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("MonthIntegerReturn", msCLASSNAME, _
"", _
mobjCOMException, mobjException)
End If
End Try
End Function

DateTime_TimeFormat

Public Shared Function TimeFormat(ByVal iHourValue As System.Int32, _
ByVal iMinutesValue As System.Int32, _
ByVal iSecondsValue As System.Int32) _
As String

Try
If gbEND_GENERAL = True Then Exit Function

TimeFormat = iHourValue & ":" & iMinutesValue & ":" & iSecondsValue

Catch objCOMException As System.Runtime.InteropServices.COMException
mobjCOMException = objCOMException
Catch objException As Exception
mobjException = objException

Finally
If gbDEBUG_GENERAL = True Or _
((Not mobjCOMException Is Nothing) Or (Not mobjException Is Nothing)) Then

Call clsError.Handle("TimeFormat", msCLASSNAME, _
"", _
mobjCOMException, mobjException)
End If
End Try
End Function

DateTime_ToString

Public Function DateTime_ToString(ByVal dtDateTime As System.DateTime, _
ByVal sDateFormat As String) _
As String
Dim sformat As String
Try
If My.Settings.USER_ERROR_OCCURRED = True Then Return "" : Exit Function

sformat = "{0:" & sDateFormat & "}"
Return System.String.Format(sformat, dtDateTime)

Catch ex As System.Exception
Call modMessages.ErrorMessage(System.Reflection.MethodBase.GetCurrentMethod, ex.Message)
Return ""
End Try
End Function

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited Top