I try to to convert string to specific datetime format.
I have string:
1431075600
and I try convert this by:
private static IFormatProvider culture = new System.Globalization.CultureInfo("en-GB");
model.DeliveryDate = DateTime.Parse(data.DeliveryDate, culture, System.Globalization.DateTimeStyles.AssumeLocal);
I got error message:
String was not recognized as a valid DateTime.
Finally I want to have datetime in format like
Friday, 8 May 2015 hour: 09:00
Your string looks like a Unix Time which is elapsed as a seconds since 1 January 1970 00:00 UTC.
. That's why, you can't directly parse it to DateTime
. You need to create a unix time first and add this value as a second.
That's why you need to add your string as a second to this value like;
var dt = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dt = dt.AddSeconds(1431075600).ToLocalTime();
And you can format your string after that with an english-based culture line InvariantCulture
;
Console.WriteLine(dt.ToString("dddd, d MMM yyyy 'h'our: HH:mm",
CultureInfo.InvariantCulture));
Result will be;
Friday, 8 May 2015 hour: 09:00
Here a demonstration
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.