Why can't it parse this:
DateTime.Parse("Tue, 1 Jan 2008 00:00:00 UTC")
It can't parse that string because "UTC" is not a valid time zone designator.
UTC time is denoted by adding a 'Z' to the end of the time string, so your parsing code should look like this:
DateTime.Parse("Tue, 1 Jan 2008 00:00:00Z");
From the Wikipedia article on ISO 8601
If the time is in UTC, add a 'Z'
directly after the time without a
space. 'Z' is the zone designator for
the zero UTC offset. "09:30 UTC" is
therefore represented as "09:30Z" or
"0930Z". "14:45:15 UTC" would be
"14:45:15Z" or "144515Z".
UTC time is also known as 'Zulu' time,
since 'Zulu' is the NATO phonetic
alphabet word for 'Z'.
Assuming you use the format "o" for your datetime so you have "2016-07-24T18:47:36Z", there is a very simple way to handle this.
Call DateTime.Parse("2016-07-24T18:47:36Z").ToUniversalTime().
What happens when you call DateTime.Parse("2016-07-24T18:47:36Z") is you get a DateTime set to the local timezone. So it converts it to the local time.
The ToUniversalTime() changes it to a UTC DateTime and converts it back to UTC time.
Just use that:
var myDateUtc = DateTime.SpecifyKind(DateTime.Parse("Tue, 1 Jan 2008 00:00:00"), DateTimeKind.Utc);
if (myDateUtc.Kind == DateTimeKind.Utc)
{
Console.WriteLine("Yes. I am UTC!");
}
You can test this code using the online c# compiler:
http://rextester.com/
I hope it helps.
or use the AdjustToUniversal DateTimeStyle in a call to
DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles)
You need to specify the format:
DateTime date = DateTime.ParseExact(
"Tue, 1 Jan 2008 00:00:00 UTC",
"ddd, d MMM yyyy HH:mm:ss UTC",
CultureInfo.InvariantCulture);
To correctly parse the string given in the question without changing it, use the following:
using System.Globalization;
string dateString = "Tue, 1 Jan 2008 00:00:00 UTC";
DateTime parsedDate = DateTime.ParseExact(dateString, "ddd, d MMM yyyy hh:mm:ss UTC", CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal);
This implementation uses a string to specify the exact format of the date string that is being parsed. The DateTimeStyles parameter is used to specify that the given string is a coordinated universal time string.
It's not a valid format, however "Tue, 1 Jan 2008 00:00:00 GMT" is.
The documentation says like this:
A string that includes time zone information and conforms to ISO 8601. For example, the first of the following two strings designates the Coordinated Universal Time (UTC); the second designates the time in a time zone seven hours earlier than UTC:
2008-11-01T19:35:00.0000000Z
A string that includes the GMT designator and conforms to the RFC 1123 time format. For example:
Sat, 01 Nov 2008 19:35:00 GMT
A string that includes the date and time along with time zone offset information. For example:
03/01/2009 05:42:00 -5:00
I've put together a utility method which employs all tips shown here plus some more:
static private readonly string[] MostCommonDateStringFormatsFromWeb = {
"yyyy'-'MM'-'dd'T'hh:mm:ssZ", // momentjs aka universal sortable with 'T' 2008-04-10T06:30:00Z this is default format employed by moment().utc().format()
"yyyy'-'MM'-'dd'T'hh:mm:ss.fffZ", // syncfusion 2008-04-10T06:30:00.000Z retarded string format for dates that syncfusion libs churn out when invoked by ejgrid for odata filtering and so on
"O", // iso8601 2008-04-10T06:30:00.0000000
"s", // sortable 2008-04-10T06:30:00
"u" // universal sortable 2008-04-10 06:30:00Z
};
static public bool TryParseWebDateStringExactToUTC(
out DateTime date,
string input,
string[] formats = null,
DateTimeStyles? styles = null,
IFormatProvider formatProvider = null
)
{
formats = formats ?? MostCommonDateStringFormatsFromWeb;
return TryParseDateStringExactToUTC(out date, input, formats, styles, formatProvider);
}
static public bool TryParseDateStringExactToUTC(
out DateTime date,
string input,
string[] formats = null,
DateTimeStyles? styles = null,
IFormatProvider formatProvider = null
)
{
styles = styles ?? DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal; //0 utc
formatProvider = formatProvider ?? CultureInfo.InvariantCulture;
var verdict = DateTime.TryParseExact(input, result: out date, style: styles.Value, formats: formats, provider: formatProvider);
if (verdict && date.Kind == DateTimeKind.Local) //1
{
date = date.ToUniversalTime();
}
return verdict;
//0 employing adjusttouniversal is vital in order for the resulting date to be in utc when the 'Z' flag is employed at the end of the input string
// like for instance in 2008-04-10T06:30.000Z
//1 local should never happen with the default settings but it can happen when settings get overriden we want to forcibly return utc though
}
Notice the use of '-' and 'T' (single-quoted). This is done as a matter of best practice since regional settings interfere with the interpretation of chars such as '-' causing it to be interpreted as '/' or '.' or whatever your regional settings denote as date-components-separator. I have also included a second utility method which show-cases how to parse most commonly seen date-string formats fed to rest-api backends from web clients. Enjoy.
Just replace "UTC" with "GMT" -- simple and doesn't break correctly formatted dates:
DateTime.Parse("Tue, 1 Jan 2008 00:00:00 UTC".Replace("UTC", "GMT"))
Not sure why, but you can wrap DateTime.ToUniversalTime in a try / catch and achieve the same result in more code.
Good luck.
Related
I have the date string like 03/10/1999 where the format is dd/MM/yyyy (pt-BR format).
And I need to convert this date for a SQL-like format yyyy-MM-dd HH:mm:ss.fff.
I tried to use Parse and ParseExact functions, but no success so far. I will let my results below...
Using Parse
var BrazilianDate = "03/10/1999";
var Parse = DateTime.Parse(BrazilianDate, new CultureInfo("pt-BR"));
Console.WriteLine("Parsed date: " + Parse);
Output: Parsed date: 10/3/1999 12:00:00 AM
No hyphens or milliseconds...
Using ParseExact
var BrazilianDate = "03/10/1999";
var ParseExact = DateTime.ParseExact(BrazilianDate, "yyyy-MM-dd HH:mm:ss.fff", new CultureInfo("pt-BR"));
Console.WriteLine(ParseExact);
output:
Run-time exception (line -1): String was not recognized as a valid
DateTime.
Stack Trace:
[System.FormatException: String was not recognized as a valid
DateTime.] at System.DateTimeParse.ParseExact(String s, String
format, DateTimeFormatInfo dtfi, DateTimeStyles style) at
System.DateTime.ParseExact(String s, String format, IFormatProvider
provider) at Program.Main()
You need to format your output with the correct format string like this:
Console.WriteLine("Parsed date: " + Parse.ToString("yyyy-MM-dd HH:mm:ss.fff"));
//Parsed date: 1999-10-03 00:00:00.000
If you don't specify a format, .NET picks whatever it thinks is the right one (which it often isn't when you're not in the US).
You also need to strictly separate between the DateTime value and its representation in string form. No matter how you format it, the value itself will stay the same.
The format string you use in the parse method represents the format of the input string.
A DateTime does not have a display format, in fact it's a numeric value representing the number of ticks since a specific Epoch.
From official documentation:
Time values are measured in 100-nanosecond units called ticks. A particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar. The number excludes ticks that would be added by leap seconds. For example, a ticks value of 31241376000000000L represents the date Friday, January 01, 0100 12:00:00 midnight.
When parsing strings, I find it's best to either use ParseExact or TryParseExact. To print our the string representation of the DateTime value, use the overload of ToString that takes in a string that represent the format you want to display.
var BrazilianDateString = "03/10/1999";
var DateTimeValue = DateTime.ParseExact(BrazilianDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(DateTimeValue.ToString("yyyy-MM-dd HH:mm:ss.fff");
This code is working for me:
DateTime dt = new DateTime();
string x = "03/10/1999 22:10:10";
dt = DateTime.Parse(x);
Console.WriteLine(dt.ToShortDateString());
Console.WriteLine(dt.ToShortTimeString());
Console.ReadLine();
Console output:
03/10/1999
22:10
Don't use that CultureInfo, DateTime can understand spanish-brazilian dates on its own
Why can't it parse this:
DateTime.Parse("Tue, 1 Jan 2008 00:00:00 UTC")
It can't parse that string because "UTC" is not a valid time zone designator.
UTC time is denoted by adding a 'Z' to the end of the time string, so your parsing code should look like this:
DateTime.Parse("Tue, 1 Jan 2008 00:00:00Z");
From the Wikipedia article on ISO 8601
If the time is in UTC, add a 'Z'
directly after the time without a
space. 'Z' is the zone designator for
the zero UTC offset. "09:30 UTC" is
therefore represented as "09:30Z" or
"0930Z". "14:45:15 UTC" would be
"14:45:15Z" or "144515Z".
UTC time is also known as 'Zulu' time,
since 'Zulu' is the NATO phonetic
alphabet word for 'Z'.
Assuming you use the format "o" for your datetime so you have "2016-07-24T18:47:36Z", there is a very simple way to handle this.
Call DateTime.Parse("2016-07-24T18:47:36Z").ToUniversalTime().
What happens when you call DateTime.Parse("2016-07-24T18:47:36Z") is you get a DateTime set to the local timezone. So it converts it to the local time.
The ToUniversalTime() changes it to a UTC DateTime and converts it back to UTC time.
Just use that:
var myDateUtc = DateTime.SpecifyKind(DateTime.Parse("Tue, 1 Jan 2008 00:00:00"), DateTimeKind.Utc);
if (myDateUtc.Kind == DateTimeKind.Utc)
{
Console.WriteLine("Yes. I am UTC!");
}
You can test this code using the online c# compiler:
http://rextester.com/
I hope it helps.
or use the AdjustToUniversal DateTimeStyle in a call to
DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles)
You need to specify the format:
DateTime date = DateTime.ParseExact(
"Tue, 1 Jan 2008 00:00:00 UTC",
"ddd, d MMM yyyy HH:mm:ss UTC",
CultureInfo.InvariantCulture);
To correctly parse the string given in the question without changing it, use the following:
using System.Globalization;
string dateString = "Tue, 1 Jan 2008 00:00:00 UTC";
DateTime parsedDate = DateTime.ParseExact(dateString, "ddd, d MMM yyyy hh:mm:ss UTC", CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal);
This implementation uses a string to specify the exact format of the date string that is being parsed. The DateTimeStyles parameter is used to specify that the given string is a coordinated universal time string.
It's not a valid format, however "Tue, 1 Jan 2008 00:00:00 GMT" is.
The documentation says like this:
A string that includes time zone information and conforms to ISO 8601. For example, the first of the following two strings designates the Coordinated Universal Time (UTC); the second designates the time in a time zone seven hours earlier than UTC:
2008-11-01T19:35:00.0000000Z
A string that includes the GMT designator and conforms to the RFC 1123 time format. For example:
Sat, 01 Nov 2008 19:35:00 GMT
A string that includes the date and time along with time zone offset information. For example:
03/01/2009 05:42:00 -5:00
I've put together a utility method which employs all tips shown here plus some more:
static private readonly string[] MostCommonDateStringFormatsFromWeb = {
"yyyy'-'MM'-'dd'T'hh:mm:ssZ", // momentjs aka universal sortable with 'T' 2008-04-10T06:30:00Z this is default format employed by moment().utc().format()
"yyyy'-'MM'-'dd'T'hh:mm:ss.fffZ", // syncfusion 2008-04-10T06:30:00.000Z retarded string format for dates that syncfusion libs churn out when invoked by ejgrid for odata filtering and so on
"O", // iso8601 2008-04-10T06:30:00.0000000
"s", // sortable 2008-04-10T06:30:00
"u" // universal sortable 2008-04-10 06:30:00Z
};
static public bool TryParseWebDateStringExactToUTC(
out DateTime date,
string input,
string[] formats = null,
DateTimeStyles? styles = null,
IFormatProvider formatProvider = null
)
{
formats = formats ?? MostCommonDateStringFormatsFromWeb;
return TryParseDateStringExactToUTC(out date, input, formats, styles, formatProvider);
}
static public bool TryParseDateStringExactToUTC(
out DateTime date,
string input,
string[] formats = null,
DateTimeStyles? styles = null,
IFormatProvider formatProvider = null
)
{
styles = styles ?? DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal; //0 utc
formatProvider = formatProvider ?? CultureInfo.InvariantCulture;
var verdict = DateTime.TryParseExact(input, result: out date, style: styles.Value, formats: formats, provider: formatProvider);
if (verdict && date.Kind == DateTimeKind.Local) //1
{
date = date.ToUniversalTime();
}
return verdict;
//0 employing adjusttouniversal is vital in order for the resulting date to be in utc when the 'Z' flag is employed at the end of the input string
// like for instance in 2008-04-10T06:30.000Z
//1 local should never happen with the default settings but it can happen when settings get overriden we want to forcibly return utc though
}
Notice the use of '-' and 'T' (single-quoted). This is done as a matter of best practice since regional settings interfere with the interpretation of chars such as '-' causing it to be interpreted as '/' or '.' or whatever your regional settings denote as date-components-separator. I have also included a second utility method which show-cases how to parse most commonly seen date-string formats fed to rest-api backends from web clients. Enjoy.
Just replace "UTC" with "GMT" -- simple and doesn't break correctly formatted dates:
DateTime.Parse("Tue, 1 Jan 2008 00:00:00 UTC".Replace("UTC", "GMT"))
Not sure why, but you can wrap DateTime.ToUniversalTime in a try / catch and achieve the same result in more code.
Good luck.
What is the name of this DateTime format:
Tue Apr 01 2014 00:00:00 GMT+0100 (GMT Daylight Time)
Is there anyway I could detect this format in code?
The reason I am asking is that I have a function with DateTime parameter, which comes in different formats, and I would like to detect the format type or name; so that I could convert them accorddingly to the simple format of dd/MM/yyyy hh:mm:ss.
The other second format I am getting is this: 2014-03-31T23:00:00.000Z.
Many thanks.
Edit
I wrote this function to convert from Tue Apr 01 2014 00:00:00 GMT+0100 (GMT Daylight Time) to dd/MM/yyyy hh:mm:ss. This function fails when the input is of type 2014-03-31T23:00:00.000Z.
I wonder how could possibly identify the type of parameter coming and convert accordingly?
public static DateTime ConvertDateObjectToDateTime(string dateToConvert)
{
var value = new DateTime();
if (!string.IsNullOrEmpty(dateToConvert))
{
int gmtIndex = dateToConvert.IndexOf("G", System.StringComparison.Ordinal);
string newDate = dateToConvert.Substring(0, gmtIndex).Trim();
value = DateTime.ParseExact(newDate, "ddd MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture);
return value;
}
return value;
}
The second is definitely UTC, however, the first could be UTC + offset or it could be Local + offset (it looks like the latter the more I examine it). The best tool you have in your armoury for parsing specific dates is the ParseExact method.
Based on your edit, I am concerned about the fact you are ignoring the timezone information. You are assuming at this point that the date is already UTC (which it may not be) and just parsing/treating it as is...
However, to answer your particular question
I wonder how could possibly identify the type of parameter coming and convert accordingly?
You don't actually need to do that, ParseExact has an overload which allows you to specify multiple formats
value = DateTime.ParseExact(newDate,
new[] { "ddd MMM dd yyyy HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss.fff'Z'" },
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
i want to calculate a checktime to the time now and get the hours.
I have a string "time" for example...
Jun 06 2013 07:23:06
and with DateTime.Now I get the Time now. The Problem is now that i can't calculate the difference :(
I need them in my Project where I get from the License Server the time from a user and I want to show the difference to now. I want show this in hours.
You can use the Parse method of the DateTIme class to parse a string as a date and the subtract that from now.
TimeSpan diff = DateTime.Now - DateTime.Parse(dateString);
var hours = diff.Hours
The above exsmple of course requires the date to be in a specific format. You can if needed use DateTIme.ParseExact and specify a specific format yourself
You need to first convert your string to DateTime. here you have custom format so you can use DateTime.ParseExact or DateTime.TryParseExact method as below
DateTime dt;
if (DateTime.TryParseExact("Jun 06 2013 07:23:06", "MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
// get difference
var inDays = (DateTime.Now - dt).Days;
}
You can use TimeSpan.Hours property like;
Gets the hours component of the time interval represented by the
current TimeSpan structure.
string dateString = "Jun 06 2013 07:23:06";
var differenceHours = (DateTime.Now - DateTime.Parse(dateString)).Hours;
Console.WriteLine(differenceHours);
Here a DEMO.
If you want to convert your custom formatted string to DateTime, you can use DateTime.ParseExact which need exact format matching between string and datetime.
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
u may try it
DataTime diff = DateTime.Now - Convert.ToDataTime(dateString);
var hours = diff.Hours
I have a DateTime stored in universal time (UTC) of value 2010-01-01 01:01:01.
I would like to display it in EST in this format 2010-01-01 04:01:01GMT-04:00, however the 'K' formatter for timezone doesn't work in ToString
Use the "zzz" format specifier to get the UTC offset. For example:
var dt = new DateTime(2010, 1, 1, 1, 1, 1, DateTimeKind.Utc);
string s = dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss \"GMT\"zzz");
Console.WriteLine(s);
Output:
2009-12-31 19:01:01 GMT-06:00
I'm in the CDT timezone. Make sure the DateTime is unambiguously DateTimeKind.Utc.
If like myself you happen to need a format like 2018-03-31T01:23:45.678-0300 (no colon in the timezone part), you can use this:
datetime.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz").Remove(26,1)
This method will return the specified time in Eastern Standard Time (as the question requested), even if EST is not the local time zone:
public string GetTimeInEasternStandardTime(DateTime time)
{
TimeZoneInfo easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTimeOffset timeInEST = TimeZoneInfo.ConvertTime(time, easternStandardTime);
return timeInEST.ToString("yyyy-MM-dd hh:mm:ss tt\" GMT\"zzz");
}
Note: I haven't tested this in a non-English OS. See the MSDN documentation on TimeZoneInfo.FindSystemTimeZoneById.
Something like this works. You could probably clean it up a bit more:
string newDate = string.Format("{0:yyyy-MM-dd HH:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"));
I think you are looking for the TimeZoneInfo class (see http://msdn.microsoft.com/en-us/library/system.timezoneinfo_members.aspx). It has many static methods to convert dates between time zones.