Convert unix time to specific date and time format with culture information
I am trying to convert a string to a specific datetime format.
I have a line:
1431075600
and I will try to convert this:
private static IFormatProvider culture = new System.Globalization.CultureInfo("en-GB");
model.DeliveryDate = DateTime.Parse(data.DeliveryDate, culture, System.Globalization.DateTimeStyles.AssumeLocal);
I got an error message:
String was not recognized as a valid DateTime.
Finally, I want to have a datetime in a format like
Friday, 8 May 2015 hour: 09:00
source to share
Your line looks like Unix Time , which has elapsed after a few seconds with 1 January 1970 00:00 UTC.
. Therefore, you cannot directly parse it to DateTime
. First you need to create unix time and add this value as second.
This is why you need to add your string as a second for this value, for example:
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 English cult line InvariantCulture
;
Console.WriteLine(dt.ToString("dddd, d MMM yyyy 'h'our: HH:mm",
CultureInfo.InvariantCulture));
The result will be:
Friday, 8 May 2015 hour: 09:00
Here . demonstration
source to share