Get DateTime on querystring - c#

there are 2 variable in my querystring:
&Start=Mon Apr 02 2012 00:00:00 GMT+0200&End=Thu Apr 26 2012 00:00:00 GMT+0200
when i try to get it
like this:
DateTime EndDate = Convert.ToDateTime(Request.QueryString["End"]);
or this:
DateTime date = DateTime.ParseExact(Request.QueryString["Start"], "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
i got the message :
The string was not recognized as a valid DateTime.
Anybody can help me?
PS: i use devexpress Component
<dx:ASPxDateEdit ID="ASPxDateEdit_Synthe_Fin" runat="server" Width="100px" ClientInstanceName="ASPxDateEdit_Synthe_Fin">
</dx:ASPxDateEdit>
<dx:ASPxButton ID="ASPxButton_Synthese" runat="server" Text="Synthese" AutoPostBack="False">
<ClientSideEvents Click="function (s, e) { e.processOnServer = false; window.open('Report/Syntehse.aspx?CientID='+ASPxComboBox_Client.GetValue()+'&Start='+ASPxDateEdit_Synth_Deb.GetValue()+'&End='+ASPxDateEdit_Synthe_Fin.GetValue());}" />
</dx:ASPxButton>
Thanks you in advance

why do you write "dd/MM/yyyy" ? it aint the format in the QS ...
it should be something like :
ddd MMM dd yyyy HH:mm:ss
--->Mon Apr 02 2012 00:00:00 GMT+0200

Convert will try different formats, but can fail, as you have seen.
When using ParseExact or TryParseExact you need to pass in a format string that directly corresponds to the string you are trying to parse.
You have use dd/MM/yyyy for Mon Apr 02 2012 00:00:00 GMT+0200. These do not correspond to each other.
Try ddd MMM dd yyyy HH:mm:ss G\MTK instead:
DateTime.ParseExact("Mon Apr 02 2012 00:00:00 GMT+0200",
"ddd MMM dd yyyy HH:mm:ss G\\MTK",
CultureInfo.InvariantCulture)
Update:
An additional issue is that the URL parameters are not URL encoded so this:
&Start=Mon Apr 02 2012 00:00:00 GMT+0200&End=Thu Apr 26 2012 00:00:00 GMT+0200
End up with the + characters being seen as spaces on the server side (since + also encodes a space on URLs).
You need to URL encode the date/time values before placing them on the URL.

DateTime.ParseExact(date, "ddd MMM dd yyyy HH:mm:ss \"GMT\"zzz", System.Globalization.CultureInfo.InvariantCulture) works on the format specified in the querystring.

Related

C# DateTime Parsing - inconsistent format

I have an example date / time string that I need to convert to datetimeoffset.
There is one huge inconsistency - if the day of the month < 10 then there is a double space between Month and Day, otherwise just a single space.
For example: 'Tue Dec 4 22:39:38 UTC 2018' and 'Tue Dec 14 22:39:38 UTC 2018'
I currently parse it using DateTimeOffset.ParseExact(dateTime, "ddd MMM dd HH:mm:ss UTC yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal) which fails for dates where the day is < 10 with the error:
FormatException: String 'Tue Dec 4 22:52:42 UTC 2018' was not recognized as a valid DateTime.
I know I can search and replace double space character with a single space, but is there a more elegant way to achieve this using the format string?
This question seemed to have a few comments (including my own erroneous one about using AllowLeadingWhite (I had meant AllowInnerWhite).
However, just using AllowInnerWhite with the existing format string still produces an error:
Console.WriteLine(DateTimeOffset.ParseExact("Tue Dec 4 22:39:38 UTC 2018", "ddd MMM dd HH:mm:ss UTC yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowInnerWhite));
Produces:
FormatException: String was not recognized as a valid DateTime.
However, looking at this from a different angle, why not change the date format itself to permit single-digit dates. Use "ddd MMM d HH:mm:ss UTC yyyy" (with a single 'd' for the actual date instead of 'dd'):
Console.WriteLine(DateTimeOffset.ParseExact("Tue Dec 4 22:39:38 UTC 2018", "ddd MMM d HH:mm:ss UTC yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowInnerWhite));
Console.WriteLine(DateTimeOffset.ParseExact("Tue Dec 11 22:39:38 UTC 2018", "ddd MMM d HH:mm:ss UTC yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowInnerWhite));
Console.WriteLine(DateTimeOffset.ParseExact("Fri Dec 14 22:39:38 UTC 2018", "ddd MMM d HH:mm:ss UTC yyyy", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowInnerWhite));
Note that your example data of 'Tue Dec 14 22:39:38 UTC 2018' will fail because Dec 14th 2018 is a Friday, not a Tuesday.
That's what the DateTimeStyles.Allow* flags are for: They instruct the parser to ignore whitespaces in the date string.
In your case the string starts with the abbreviated weekday name, so the flag minimally sufficient to ignore the additional space in case of a one-digit day number is
DateTimeStyles.AllowInnerWhite
This and related flags are documented here: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.datetimestyles?view=netframework-4.7.2

Convert timezone specific datetime string to datetime in C#

How can I convert Tue, 01 Nov 2016 02:00 PM EET datetime string to DateTime in C#? What is a good practice to do it?
Use DateTime.TryParseExact with a format string that represents a generic datetime.
If you can have multiple formats then use the DateTime.TryParseExact overload that takes an array of formats.
You can find all the format strings here:
Custom Date and Time Format Strings
For example, "Tue" is represented by "ddd", "Nov" by "MMM" etc.
NOTE: The string formats are case sensitive so while "M" represents the month number, "m" represents the minute number. Getting them mixed up will cause the parse to fail.
By replacing timezone abbreviation with zone offset you can convert using DateTime.ParseExact
string date = "Tue, 01 Nov 2016 02:00 PM EET";
DateTime dt = DateTime.ParseExact(date.Replace("EET", "+2"), "ddd, dd MMM yyyy hh:mm tt z", CultureInfo.InvariantCulture);
and if you want more safer way by checking exception then you can using DateTime.TryParseExact method
Use DateTime.TryParseExact where the format string is built using this table.
Custom date and time formats does not recognize timezone abbrevations. You need to escape them as a string literal delimiter.
var dt = DateTime.ParseExact("Tue, 01 Nov 2016 02:00 PM EET",
"ddd, dd MMM yyyy hh:mm tt 'EET'",
CultureInfo.InvariantCulture);
dt.Dump();

Format DateTime.ParseInfo

Good morning,
I have problem with format of datetime which comes in format "Mon Jun 15 2015 00:00:00 GMT+0200 (Central Europe Daylight Time)".
var value = "Mon Jun 15 2015 00:00:00 GMT+0200 (Central Europe Daylight Time)";
DateTime.Parse(value) throws CastException.
I have also tried use ParseExact method which is more powerfull but still without access
var date = DateTime.ParseExact(
"Mon Jun 15 2015 00:00:00 GMT+0200 (Central Europe Daylight Time)",
"ddd MMM dd yyyy HH:mm:ss 'GMT+0000 (Central Europe Daylight Time)'",
CultureInfo.InvariantCulture);
var date = DateTime.ParseExact(
"Mon Jun 15 2015 00:00:00 GMT+0200 (Central Europe Daylight Time)",
"ddd MMM dd yyyy HH:mm:ss 'GMT+0000 (GMT Standard Time)'",
CultureInfo.InvariantCulture);
Everything fails ...
I also wanted try hack and make a SubString(4,11) but IE returns diferent format than chrome so it's not usable ...
I am sending it from kendo filter in grid.
So my questions are:
1) Is there way how to parse this format vith C#?
2) Is there way how to say to Kendo filter "send another format"? Format and ParseFormats ignores me ...
My expected format in which I want format is dd.MM.yyyy ..
Thank you for help
Edit:
My kendo code looks like:
.....
{
field: "DateField",
type: "date",
width: "110px",
template: function (e) {
return $.format.date(e.DateField, "dd.MM.yyyy");
},
filterable: {
extra: true,
operators: {
date: {
gt: "Is greater than",
lt: "Is less than"
}
},
ui: function (element) {
element.kendoDatePicker({
format: "dd.MM.yyyy"
});
}
}
},
With ParseExact():
using System.Globalization;
var value = "Mon Jun 15 2015 00:00:00 GMT+0200 (Central Europe Daylight Time)";
var trimedValue = value.Substring(0, value.IndexOf(" ("));
var dateTime = DateTime.ParseExact(trimedValue, "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz", CultureInfo.InvariantCulture);
Your DateTime.Parse code fails because your CurrentCulture doesn't have a standard date and time format for that string. None of culture can have this kind of format. A DateTime timezone awareness by the way.
Your ParseExact examples fails because; first one has +0200 for offset but your string has +0000. This remains for second one and also your timezones are different. (Central Europe Daylight Time and GMT Standard Time)
Since you have UTC offset part in your string, I would choose DateTimeOffset parsing with "K" format specifier instead like;
string s = "Mon Jun 15 2015 00:00:00 GMT+0200 (Central Europe Daylight Time)";
DateTimeOffset dto;
if (DateTimeOffset.TryParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT'K '(Central Europe Daylight Time)'",
CultureInfo.InvariantCulture,
DateTimeStyles.AssumeUniversal, out dto))
{
Console.WriteLine(dto);
}
Now you have a DateTimeOffset as {15.06.2015 00:00:00 +02:00} and I think this might be the best thing you can get.

How can I format a date in RFC format?

I need to format a date in RFC format for an RSS feed. I have tried taking my date and adding the following:
.ToString("ddd, dd mm yyyy HH:mm:ss zzz")
However it seems this is not valid. Any help would be great! The end result I need is, for example, Mon, 01 Jan 2013 GMT
Your question is a bit confusing because your example lacks any time (but has a time-zone).
Nevertheless, try using MMM for month:
.ToString("ddd, dd MMM yyyy HH:mm:ss zzz")
If you don't want time, simply omit that section from the string:
.ToString("ddd, dd MMM yyyy")
But since you mentioned you're generating a string for an RSS feed, you could just use the "R" format specifier to generate a RFC1123-format date / time string:
.ToString("R")

DateTime parsing complicated strings C#

I am trying to parse date-strings to DateTime objects with the following format:
Tue, 30 Oct 2012 09:51:20 +0000
What I have tried so far is many different variants with DateTime.ParseExact().
I have tried:
DateTime.ParseExact("Mon, 29 Oct 2012 12:13:51 +0000",
"ddd, dd MM yyyy hh':'mm':'ss zzz",
CultureInfo.InvariantCulture);
With thousands different formats as second parameter, using null instead of InvarantCulture as third parameter etc etc. I can't get it to work. How should I parse a string like this?
Many thanks.
How about
var s = "Tue, 30 Oct 2012 09:51:20 +0000";
DateTime.ParseExact(s, "ddd, dd MMM yyyy hh:mm:ss zzz", CultureInfo.InvariantCulture)
The month (Oct) is actually MMM, not MM, and the time (09:51:20) should be hh:mm:ss instead of hh':'mm':'ss.
The correct parsing is
DateTime.ParseExact("Mon, 29 Oct 2012 12:13:51 +0000", "ddd, dd MMM yyyy HH:mm:ss K", CultureInfo.InvariantCulture);
Take a look here

Categories

Resources