public int Get4LetterYear(int twoLetterYear)
{
int firstTwoDigits =
Convert.ToInt32(DateTime.Now.Year.ToString().Substring(2, 2));
return Get4LetterYear(twoLetterYear, firstTwoDigits);
}
public int Get4LetterYear(int twoLetterYear, int firstTwoDigits)
{
return Convert.ToInt32(firstTwoDigits.ToString() + twoLetterYear.ToString());
}
public int Get2LetterYear(int fourLetterYear)
{
return Convert.ToInt32(fourLetterYear.ToString().Substring(2, 2));
}
'Create a var. named rightNow and set it to the current date/time
Dim rightNow as DateTime = DateTime.Now
Dim s as String 'create a string
s = rightNow.ToString("MMM dd, yyyy")
public static class DateTimeExtensions
{
public static int ToYearLastTwoDigit(this DateTime date)
{
var temp = date.ToString("yy");
return int.Parse(temp);
}
}
然后,您可以在使用 DateTime对象的任何地方调用此方法,例如..。
var dateTime = new DateTime(2015, 06, 19);
var year = cob.ToYearLastTwoDigit();