Convert DateTime Format to Tuesday, 03 May 2016 - c#

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");

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);

Parsing string "Sat, 14 Jan 2017 12:12:12 Europe/Warsaw" to DateTime

I need to parse the string
Sat, 14 Jan 2017 12:12:12 Europe/Warsaw
to DateTime
I've tried:
var datestring = "Sat, 14 Jan 2017 12:12:12 Europe/Warsaw";
DateTime.TryParse(datestring, out expDt);
but it's not working.
Thanks in advance for any help.
I think you can do something like this:
string datestring = "Sat, 14 Jan 2017 12:12:12 Europe/Warsaw";
// remove "Europe/Warsaw" because it wont be used.
datestring = datestring.Substring(0, datestring.LastIndexOf(' '));
// now datestring looks like "Sat, 14 Jan 2017 12:12:12"
// so you should adapt the format:
string dateFormat = "ddd, dd MMM yyyy HH:mm:ss";
// now you can use DateTime.ParseExact to retrieve DateTime object
DateTime dt = DateTime.ParseExact(datestring, dateFormat, System.Globalization.CultureInfo.InvariantCulture);
This should parse to correct DateTime object.
Calling dt.ToString() should return something like 14.01.2017 12:12:12
EDIT:
For everyone else that assumes that DateTime object is somewhat aware of TimeZone or is it dependant on it. Please read this answer Get timezone from DateTime
That's why for me it was useless to extract TimeZone from the string. Because it would have no impact on DateTime object itself. If ( but I doubt that ) someone has the same issue and need this informations then here's the example :
string datestring = "Sat, 14 Jan 2017 12:12:12 Europe/Warsaw";
// remove "Europe/Warsaw" because it wont be used.
string datestr = new string(datestring.Take(datestring.LastIndexOf(' ')));
// now datestring looks like "Sat, 14 Jan 2017 12:12:12"
// so you should adapt the format:
string dateFormat = "ddd, dd MMM yyyy HH:mm:ss";
// now you can use DateTime.ParseExact to retrieve DateTime object
DateTime dt = DateTime.ParseExact(datestring, dateFormat, System.Globalization.CultureInfo.InvariantCulture);
string timezonestr = new string(datestring.Skip(datestring.LastIndexOf(' ') + 1));
try {
TimeZoneInfo timzeone = TimeZoneInfo.FindSystemTimeZoneById(timezonestr);
} catch { /* probably an error because there's no timezone called Europe/Warsaw */ }

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);

Need to convert following string into time format

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.

Format String to Datetime with Timezone

I have a string s = "May 16, 2010 7:20:12 AM CDT that i want to convert into a DateTime object. In the code below i get a Date format cannot be converted error when attempting to parse the text with a known format.
timeStamp = matches[0].Groups[1].Value;
dt = DateTime.ParseExact(timeStamp, "MMM dd, yyyy H:mm:ss tt", null);
The timezone comes in as CDT UTC... and i think is whats causing the problem or my format?
Central Daylight Time
Try this:
string dts = "May 16, 2010 7:20:12 AM CDT";
DateTime dt =
DateTime.ParseExact(dts.Replace("CDT", "-05:00"), "MMM dd, yyyy H:mm:ss tt zzz", null);
EDIT:
For daylight savings time please consider DateTime.IsDaylightSavingTime and TimeZone.CurrentTimeZone
Custom Date and Time Format Strings
Make sure the DateTime is unambiguously DateTimeKind.Utc. Avoid "GMT", it is ambiguous for daylight saving.
var dt = new DateTime(2010, 1, 1, 1, 1, 1, DateTimeKind.Utc);
string s = dt.ToLocalTime().ToString("MMM dd, yyyy HH:mm:ss tt \"GMT\"zzz");
it's gives output : Dec 31, 2010 19:01:01 pm GMT-06:00
For more detail refer this Link
I convert my date string with timezone "Thu, 22 Sep 2022 06:38:58 +0200" using "ddd, dd MMM yyyy HH:mm:ss zzz":
var dateString = "Thu, 22 Sep 2022 06:38:58 +0200";
string[] acceptedDateFormats = { "ddd, dd MMM yyyy HH:mm:ss zzz" };
var dateParsed = DateTime.TryParseExact(dateString, acceptedDateFormats,
CultureInfo.InvariantCulture, DateTimeStyles.None, out var date);

Categories