.NET (C #) Time Date String (String) Format Convert to DateTime Abnormal Announcement

The original text goes to: https://www.cjavapy.com/article/310/

  1. The time and date string (String) is converted into Datetime method

    1) Convert.ToDateTime(string)

      string The format is required and must be yyyy-MM- dd hh:mm:ss

     2) DateTime.ParseExact()

System.Globalization. CultureInfo Culinfo = CultureInfo.GetCultureInfo("en-us");
string dateString = "Nov 15 2005 12:00:00:000AM";
DateTime dt = DateTime.ParseExact(dateString, "MMM dd yyyy hh:mm:ss:ffftt", Culinfo) ;

or

DateTime dt = DateTime.ParseExact(dateString, < span class="hljs-string">"MMM dd yyyy hh:mm:ss:ffftt", Culinfo);

  2. Abnormal conversion of string (String) to Datetime Error report solution

Error message: System.FormatException: “String’Nov 15 2005 12:00:00:000AM’ was not recognized as a valid DateTime.” (String is not recognized as a valid DateTime).

Reason for the error: When this error message appears, it is basically the converted format placeholder (“MMM dd yyyy hh:mm:ss:ffftt”) does not match the time string, please check carefully . You can also use format placeholders to print out for comparison, the code is as follows:

Console.WriteLine(DateTime.Now.ToString( "MMM dd yyyy hh:mm:ss:ffftt"));

or

System.Globalization.CultureInfo Culinfo = CultureInfo .GetCultureInfo("en-us");
Console.WriteLine(DateTime.Now.ToString("MMM dd yyyy hh:mm :ss:ffftt", Culinfo));

Special attention:

en-us: United States
zh-cn: China

1) MM and M are numeric months, MMM is English or Chinese month, mainly see System.Globalization.CultureInfo Culinfo = CultureInfo.GetCultureInfo(" en-us") is it Chinese or foreign.

2) tt represents 12 hours in the morning or afternoon, whether it is English or Chinese depends on System.Globalization.CultureInfo Culinfo = CultureInfo.GetCultureInfo("en-us").

3) The decimal precision of fff representing seconds is three digits.

3. Description of string placeholders

DateTime.ToString(format):

The following formats can only be used individually and represent specific formats:

p>

d ShortDatePattern

D LongDatePattern

f Full date and time (long date and short time)

F FullDateTimePattern (long date and long time )

g Regular (short date and short time)

G Regular (short date and long time)

m, M MonthDayPattern

r, R RFC1123Pattern

s SortableDateTimePattern using local time (based on ISO 8601)

t ShortTimePattern

T LongTimePattern

u UniversalSortableDateTimePattern Format used to display universal time

U Complete date and time using universal time (long date and long time)

y, Y YearMonthPattern

The following format It can be used in combination to format different date display formats:

d a certain day of the month. One-digit dates have no leading zeros.

dd The day of the month. One-digit dates have a leading zero.

ddd The abbreviated name of the day of the week, defined in AbbreviatedDayNames.

dddd The full name of the day of the week, defined in DayNames.

M month number. The one-digit month has no leading zero.

MM month number. One-digit months have a leading zero.

MMM The abbreviated name of the month, defined in AbbreviatedMonthNames.

MMMM The full name of the month, defined in MonthNames.

y does not include the year of the era. If the year without the era is less than 10, the year without leading zeros is displayed.

yy does not include the year of the era. If the year without the era is less than 10, the year with leading zeros is displayed.

yyyy includes the four-digit year of the era.

gg the period or era. If the date to be formatted does not have an associated period or epoch string, then the pattern is ignored.

h The hour in a 12-hour clock. The one-digit hour has no leading zero.

hh The hour in 12-hour format. One-digit hours have leading zeros.

H The hour in a 24-hour clock. The one-digit hour has no leading zero.

HH The hour in a 24-hour clock. One-digit hours have leading zeros.

m minutes. One-digit minutes without leading zeros.

mm minutes. One-digit minutes have a leading zero.

s seconds. One-digit seconds without leading zeros.

ss seconds. One-digit seconds have a leading zero.

The decimal precision of f seconds is one digit. The remaining numbers are truncated.

The decimal precision of ff seconds is two digits. The remaining numbers are truncated.

The decimal precision of fff seconds is three digits. The remaining numbers are truncated.

ffff The decimal precision of seconds is four digits. The remaining numbers are truncated.

The decimal precision of fffff seconds is five digits. The remaining numbers are truncated.

ffffff The decimal precision of seconds is six digits. The remaining numbers are truncated.

fffffff The decimal precision of seconds is seven digits. The remaining numbers are truncated.

t The first character of the AM/PM indicator defined in AMDesignator or PMDesignator (if it exists).

tt AM/PM indicator defined in AMDesignator or PMDesignator (if it exists).

z Time zone offset (“+” or “-” is followed by hours only). The one-digit hour has no leading zero. For example, Pacific Standard Time is “-8”.

zz Time zone offset (“+” or “-” is followed by hours only). One-digit hours have leading zeros. For example, Pacific Standard Time is “-08”.

zzz The full time zone offset (“+” or “-” followed by hours and minutes). One-digit hours and minutes have leading zeros. For example, Pacific Standard Time is “-08:00”.

: The default time separator defined in TimeSeparator.

/ The default date separator defined in DateSeparator.

%c where c is the format mode (if used alone). If the format pattern is combined with literal characters or other format patterns, the “%” character can be omitted.

“c where c is any character. The character is displayed as it is. To display the backslash character, please use “”””.

Use the format in String.Format Transformation:

These structural objects can also be formatted in the String.Format method. These formatted formats are contained in “{}” and separated by “:”. “:” is preceded by String The index value of .Format is the same as general formatting, and after “:” is the formatting type of these structures. If placeholders are used, they are separated from the index by “,”, a positive number means right-aligned, a negative number It means left-aligned, and the absolute value means the number of characters occupied, for example:

string.Format("the value is {0,7:f3}",123.45);
//result the value is 123.450

Example: ToString() is converted to date format:

 DateTime datetime = System.DateTime.Now;
Console.WriteLine(datetime.ToString("d"));// means ShortDatePattern Specific format.
Console.WriteLine(datetime.ToString("yyyy/MM/dd hh:mm:ss"));//combination format

Leave a Comment

Your email address will not be published.