Need to convert following string into time format - c#

I have the following two dates in string format.
1. 06 Mar 2013
2. 26 Mar 2013
I need to compare those two dates i.e if (06 Mar 2013 < 26 Mar 2013)
Is there any built-in function to convert string into C# Date and Time format?

You need to parse these two dates to DateTime Object, using DateTime.ParseExact with format dd MMM yyyy and then compare both.
string str1 = "06 Mar 2013";
string str2 = "26 Mar 2013";
DateTime dt1 = DateTime.ParseExact(str1, "dd MMM yyyy", null);
DateTime dt2 = DateTime.ParseExact(str2, "dd MMM yyyy", null);
if(dt1 < dt2)
{
//dt1 is less than dt2
}
You can also use the format d MMM yyyy, with single d which would work for both single digit and double digit day (e.g. 02 ,2 and 12 etc)

Yes there is. Try DateTime.Parse and DateTime.ParseExact methods. Here is the code sample:
string first = "06 Mar 2013";
string second = "26 Mar 2013";
DateTime d1 = DateTime.Parse(first);
DateTime d21 = DateTime.Parse(second);
var result = d1 > d21; //false

Use DateTime.ParseExact in the following way:
DateTime dt = DateTime.ParseExact(str, "dd MMM yyyy", CultureInfo.InvariantCulture);
Demo
CultureInfo.InvariantCulture is needed to ensure that it will be parsed successfully even if the current culture does not have english month names.

Related

C# - How to parse datetime string "Mar 25 2014 10:15:58:757AM"

How to parse a this datetime string in c#
Mar 25 2014 10:15:58:757AM
I have tried this, but not working.
DateTime val = DateTime.ParseExact(dateTimeStringValue, "MMM dd yyyy hh:mm:ss:fffTT", CultureInfo.InvariantCulture);
Custom date and time format strings are case sensitive.
That's why you should use tt instead of TT.
DateTime.ParseExact("Mar 25 2014 10:15:58:757AM",
"MMM dd yyyy hh:mm:ss:ffftt",
CultureInfo.InvariantCulture);

Convert DateTime Format to Tuesday, 03 May 2016

In my asp.net page i want to display date in this format Tuesday, 03 May 2016 but when i retrieve data from sql server its showing 3/05/2016 12:00:00 AM
How to convert 3/05/2016 12:00:00 AM - to - Tuesday, 03 May 2016 in C# Code Behind File
Actually this is my code :
string strAccountCreatedDate = ds.Table[0].Rows[0]["AccountCreatedDate"].ToString();
strAccountCreatedDate = 3/05/2016 12:00:00 AM
Now i want to Convert strAccountCreatedDate Format to Tuesday, 03 May 2016 in through C# Coding
Try this
DateTime dt = DateTime.ParseExact("03/05/2016 12:00:00 AM", "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string newdate = dt.ToString("dddd, dd MMM yyyy");
Console.WriteLine(newdate);
You should go through this link for conversion in any date format
DateTime dt = DateTime.Now;
string date = String.Format("{0:dddd,dd MMM yyyy}", dt);
Response.Write(date);
Try this :-
string strAccountCreatedDate = ds.Table[0].Rows[0]["AccountCreatedDate"].ToString();
DateTime dt = Convert.ToDateTime(strAccountCreatedDate);
string Formatteddate = dt.ToString("dddd, dd MMM yyyy");

DateTime Parse ANSI C timestamp

I am trying to parse Date/Time formats in HTTP/1.1 headers as specified in RFC2616
How do I parse an ANSI C timestamp in C#?
The closest I get is:
string dateFormat = "ddd MMM d HH':'mm':'ss yyyy";
DateTime time = DateTime.ParseExact("Mon Jan 2 15:04:05 2006", dateFormat, CultureInfo.InvariantCulture);
The problem lies in "d" which does not accept a leading space in case it is a single digit date. And "dd" requires a leading 0 instead.
Is there any easy way around, or maybe a library that already handles the three allowed date/time formats in HTTP/1.1?
How about using DateTime.TryParseExact overload that takes string[] as a format parameter with AllowInnerWhite style?
string s = "Sun, 06 Nov 1994 08:49:37 GMT";
DateTime dt;
var formats = new[]
{
"ddd, dd MMM yyyy HH:mm:ss 'GMT'",
"dddd, dd-MMM-yy HH:mm:ss 'GMT'",
"ddd MMM d HH:mm:ss yyyy"
};
if(DateTime.TryParseExact(s, formats, CultureInfo.InvariantCulture,
DateTimeStyles.AllowInnerWhite, out dt))
{
//
}

How to format string to datetime?

I have devexpress dateedit object and I send selected date to controller from clientside but i cant convert my string date value to datetime value
When I try I get this error => string was not recognized as a valid DateTime
my string date value => Thu Aug 28 2014 00:00:00 GMT+0300 (Turkey Daylight Time)
Convert Code =>
DateTime startDate = DateTime.ParseExact(sDate, "ddd MMM d yyyy HH:mm:ss zzzz", CultureInfo.InvariantCulture);
How should I do format this string ?
You need to "escape" unrecognized symbols with single quote:
var sDate = "Thu Aug 28 2014 00:00:00 GMT+0300 (Turkey Daylight Time)";
var format = "ddd MMM dd yyyy HH:mm:ss 'GMT'zzzz '(Turkey Daylight Time)'";
DateTime startDate = DateTime.ParseExact(sDate, format, CultureInfo.InvariantCulture);
Console.WriteLine(startDate);
prints:
8/28/2014 12:00:00 AM
Works well with single d in third group, added one just for clarity.
Single or double quotes denote Literal string delimiter. You can read and check more examples at this msdn article on DateTime formats
First convert date string to date and then date to ISO and send it to server. That would work.
var date = new Date("Thu Aug 28 2014 00:00:00 GMT+0300")
var sDate = date.toISOString();
Try removing the unknown format with Regex first.
var sDate = #"Thu Aug 28 2014 00:00:00 GMT+0300 (Turkey Daylight Time)";
var sDateOnly = Regex.Replace(sDate, #"\s*(\(.*\))", m => string.Empty);
var f = #"ddd MMM d yyyy HH:mm:ss \G\M\Tzzzz";
DateTime startDate = DateTime.ParseExact(sDateOnly, f, CultureInfo.InvariantCulture);

C# How to convert irregular date and time String into DateTime?

I have a program which converts a irregular date and time string into a system DateTime.
However as the system does not recognize irregular strings, the method .ParseExact, toDateTime and TryParse has not worked.
There are only 2 types of date time strings that the program needs to convert:
Thu Dec 9 05:12:42 2010
Mon Dec 13 06:45:58 2010
Please note that the single date has a double spacing which I have used the .replace method to convert the single date to Thu Dec 09 05:12:42 2010.
May someone please advise on the codes? Thanks!
The codes:
String rb = re.Replace(" ", " 0");
DateTime time = DateTime.ParseExact(rb, "ddd MMM dd hh:mm:ss yyyy", CultureInfo.CurrentCulture);
Console.WriteLine(time.ToString("dddd, dd MMMM yyyy HH:mm:ss"));
I would really avoid regex and use what's already built-in .NET (TryParseExact method and date formats):
DateTime result;
string dateToParse = "Thu Dec 9 05:12:42 2010";
string format = "ddd MMM d HH:mm:ss yyyy";
if (DateTime.TryParseExact(
dateToParse,
format,
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces,
out result)
)
{
// The date was successfully parsed => use the result here
}
You should capture the parts of your datetime into capture groups in the Match Object, then reconstitute them any way you want.
You can use this Regex statement with named groups to make it easier
((?<day>)\w{3})\s+((?<month>)\w{3})\s+((?<date>)\d)\s((?<time>)[0-9:]+)\s+((?<year>)\d{4})
This is sample code which you can try:
var str = "Thu Dec 9 06:45:58 2010";
if (str.IndexOf(" ") > -1)
{
str = str.Replace(" ", " ");
DateTime time = DateTime.ParseExact(str, "ddd MMM d hh:mm:ss yyyy", null);
}
else
{
DateTime time = DateTime.ParseExact(str, "ddd MMM dd hh:mm:ss yyyy", null);
}

Categories