How to convert the following string to date string again? - c#

I have a string passed from kendo grid filter as follows,
2018-05-01T18:30:00.000Z
i need to conver the above to this format,
May 05 2018
i tried with the following,
string date = (DateTime.ParseExact(constant.ToString(), "MMM dd yyyy", CultureInfo.InvariantCulture)).ToString("MMM dd yyyy");
bit its not working as expected

The format of the call to DateTime.ParseExact is not the format of the date that you're passing. You could change the format, or call DateTime.Parse directly as that format is recognized by it:
String dateString = "2018-05-01T18:30:00.000Z";
DateTime date1 = DateTime.Parse(dateString);
DateTime date2 = DateTime.ParseExact(dateString, "yyyy-MM-ddTHH:mm:ss.fffZ", null);
Console.WriteLine(date1.ToString("MMM dd yyyy"));
Console.WriteLine(date2.ToString("MMM dd yyyy"));

All you need is the following. And please note that the datetime string actually represents May 1 2018, not May 5 2018 as mentioned in the question.
var d = DateTime.Parse("2018-05-01T18:30:00.000Z");
Console.WriteLine(d.ToString("MMM dd yyyy"));

Related

Parsing date form dd-MM-yyyy to dd MMM yyyy

I want to parse dd-MM-yyyy date format into dd MMM yyyy I get the reference but it cannot convert date in a proper manner it mismatches the date and month.
Suppose my date is
string dat = "11-01-2019"
and I am using
string date = DateTime.Parse(dat).ToString("dd MMM yyyy");
but it returns 01 Nov 2019. But actually, it is 11 Jan 2019 because the format of date is dd-MM-yyyy I don't understand how to correct it any method of parsing I am using it returns 01 Nov 2019 that is wrong. Please help me to resolve the issue
You'll need to specify the culture the date is formatted for. I'm assuming UK here:
var ukCulture = System.Globalization.CultureInfo.GetCultureInfo("en-gb");
string dat = "11-01-2019";
string date = DateTime.Parse(dat, ukCulture).ToString("dd MMM yyyy");
Console.WriteLine(date);
Try it online
Note that you'll get a FormatException if you enter an invalid date here, so it might be better to use TryParse:
var ukCulture = System.Globalization.CultureInfo.GetCultureInfo("en-gb");
string dat = "11-01-2019";
DateTime parsedDate;
if (DateTime.TryParse(dat, ukCulture, System.Globalization.DateTimeStyles.None, out parsedDate))
{
string date = parsedDate.ToString("dd MMM yyyy");
Console.WriteLine(date);
}
else
{
Console.WriteLine("invalid date");
}
Try it online
Adding to John's great answer, here's a list of CultureInfo codes for reference:
http://www.csharp-examples.net/culture-names/

Display date time with hours and minutes

I am trying to display date time as follows Wednesday, 05 May 2014 21:25
I tried the following but when using ToLongDateString I am not getting time, this is my code
DateTime date = DateTime.Now;
string formattedDate = date.ToLongDateString();
string fDate = date.ToString("MMMM dd, yyyy,H:mm");
Response.Write(formattedDate);
Date string does not include time. That's why it called date string. Here is your desired format:
DateTime date = DateTime.Now;
string formattedDate = date.ToString("dddd, dd MMMM yyyy HH:mm");
// Wednesday, 07 May 2014 12:05
ToLongDateString does not contain the time, as the time is not part of the date.
See HERE for some details:
Current culture: "en-US"
Long date pattern: "dddd, MMMM dd, yyyy" Long date string:
"Wednesday, May 16, 2001"
Long time pattern: "h:mm:ss tt" Long time string: "3:02:15 AM"
Short date pattern: "M/d/yyyy" Short date string: "5/16/2001"
Short time pattern: "h:mm tt" Short time string: "3:02 AM"
Also HERE and HERE on all the possiblities with ToString for DateTime.
You possibly want to use ToString("F"):
The "F" standard format specifier represents a custom date and time
format string that is defined by the current
DateTimeFormatInfo.FullDateTimePattern property. For example, the
custom format string for the invariant culture is "dddd, dd MMMM yyyy
HH:mm:ss".
You need to use the string dddd, dd MMM yyyy HH:mm.
string fDate = DateTime.Now.ToString("ddddd, dd MMMM yyyy HH:mm");
Response.Write(fDate );
Also, your code is outputting formattedDate not the fDate value.
try this way
DateTime time = DateTime.Now; // Use current time
string format = "dddd, d MMM yyyy HH:mm"; // Use this format
Console.WriteLine(time.ToString(format)); // Write to console
for more details visit below page
http://www.dotnetperls.com/datetime-format
Your can try this
DateTime date = DateTime.Now;
string formattedDate = date.ToLongDateString();
string fDate = date.ToString("dddd MMMM dd, yyyy hh:mm");
Response.Write(fDate);
This format should work:
DateTime date = DateTime.Now;
string formattedDate = date.ToString("f");
// January 13, 2023 5:00 PM
Personally, I like the format that just doing ToString() gives me e.g
HelperLib.LogMsg("Job Ran at + " + DateTime.Now.ToString();
// Job Ran at 21/01/2023 21:12:59
You can change the format with hh:mm if you don't want seconds as people have shown you above, however this format is exactly what I want and need. If I wanted the day name or month name I would use the formatting people have shown you above but for mew this gives me the Date and time and any variable that is a date format it works on e.g
DateTime raceDateTime = Convert.ToDateTime(RecordsetRow["RaceDateTime"]);
Console.WriteLine("racedatetime = " + raceDateTime.ToString();
and the same output...
The following should work:
string formattedDate = date.ToLongDateString();
formattedDate += date.ToString(" h:mm");

Error : String was not recognized as a valid DateTime while converting to date format in c#

Here is the date time format i'm trying to format.I'm getting this date format from twitter apis
string date = "Thu Jul 18 17:39:53 +0000 2013"
i tried
Convert.ToDateTime(date).ToString("dd/MM/yyyy")
But it says String was not recognized as a valid DateTime.
This works:
DateTime.ParseExact(dtStr, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture)
ParseExact and TryParseExact allows to use a custom format string. ddd is the abbreviated day name, MMM the abbreviated month name, dd the day number, HH hours in 24h clock format, mm minutes, ss seconds, zzzz the time-zone and yyyy the years.
I have used CultureInfo.InvariantCulture to specify that the current culture is not used but InvariantCulture which is similar to "en-US".
Demo
works but after getting date from your line of code i tried to do
date.ToString("dd/mm/yyyy") but get the string as 12-12-2013, no
slashes
/ is a replacement character for your current culture's date-separator which is obviously -. So also use CultureInfo.InvariantCulture to specify that the separator should be used without using your current culture:
string result = dateTime.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);
See: The "/" Custom Format Specifier
Try this
DateTime.ParseExact(YourDate, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture)
Its better to use Invariant culture than Current culture
You are trying to convert a non-standard format, so use this:
string dateStr = "Thu Jul 18 17:39:53 +0000 2013";
DateTime date = DateTime.ParseExact(dateStr, "ddd MMM dd h:mm:ss KKKK yyyy", System.Globalization.CultureInfo.InvariantCulture);
Or build the correct format for your input.
How about like;
string date = "Thu Jul 18 17:39:53 +0000 2013";
DateTime dt = DateTime.ParseExact(date, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Output will be;
18.07.2013 20:39:53
K for time zone information in here.
Check out for more information;
Custom Date and Time Format Strings
Your date string needs to be this:
Thu Jul 18 2013 17:39:53 +0000
Whatever is producing your string needs to have the year value after the month and day and before the time, like above.
string date = "Thu Jul 18 2013 17:39:53 +0000";
var theDate = Convert.ToDateTime(date);
Note: This will produce a valid .NET DateTime object.
UPDATE:
If you cannot change the string produced, then use the ParseExact method with a custom format, like this:
string date = "Thu Jul 18 17:39:53 +0000 2013";
var theDate = DateTime.ParseExact(date, "ddd MMM dd H:mm:ss zzz yyyy", CultureInfo.InvariantCulture);
Try using DateTime.ParseExact.
string date = "Thu Jul 18 17:39:53 +0000 2013"
DateTime date = DateTime.ParseExact(date, "dd/MM/yyyy", null);
this.Text="22/11/2009";
DateTime date = DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);

Parse DateTime using C# [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse Datetime string
I'm trying to parse the following DateTime in C#:
string date = "Wed Jul 25 19:41:36 2012 +0200"
DateTime result = DateTime.Parse(date);
And I'm getting the following error:
System.FormatException : String was not recognized as a valid DateTime.
Anybody knows what is the problem here?
You can use DateTime.ParseExact() for that. For example
UPDATED:
string dateString = "Your date";
string format = "ddd MMM dd HH:mm:ss yyyy %K";
DateTime dateTime = DateTime.ParseExact(dateString, format,
CultureInfo.InvariantCulture);
Console.WriteLine(dateTime);
Documentation Here and DateTime string formatting options here.
You can also view here {Complr.com}
You need to specify the parsing format as that is non-standard. DateTime.ParseExact allows you to specify the format.
Something like this will work, however I've yet to verify if that time-zone part is working correctly, seems to give me a date/time at 1800 hrs... Ah this is because where I am it is BST (GMT +1).
static void Main(string[] args)
{
string date = "Wed Jul 25 19:41:36 2012 +0200";
string format = "ddd MMM dd HH:mm:ss yyyy %K";
//string format = "ddd MMM dd HH:mm:ss yyyy zzz"; // Also works.
DateTime dateTime = DateTime.ParseExact(date, format, CultureInfo.InvariantCulture);
Console.ReadLine();
}
DateTime string formatting options are documented here, you can create a parse string using any combination of these to parse a DateTime successfully.
Another example of this can be found here: Parse DateTime From Odd Format

How do I parse a string into a DateTime, with a specific format?

I have a source where dates comes in this string form:
Sat Sep 22 13:15:03 2018
Is there an easy way I can parse that into a DateTime in C#? I've tried with DateTime.(Try)Parse, but it doesn't seem to recognize this specific format...
You should prefer DateTime.ParseExact and TryParseExact; these methods let you specify the expected format(s) in your program.
The DateTime.Parse and TryParse methods are dangerous because they accept the current date/time format configured on the machine where the code is running -- which the user can change from the default -- along with a couple of culture-neutral formats. In other words, the user can change settings in Control Panel and break Parse/TryParse.
This works:
DateTime dt = DateTime.ParseExact ("Sat Sep 22 13:15:03 2018", "ddd MMM d HH:mm:ss yyyy", null)
Try DateTime.ParseExact
This code takes your date string and applies a format to create a DateTime object.
CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "Sat Sep 22 13:12:03 2018";
string format = "ddd MMM dd HH':'mm':'ss yyyy";
DateTime result = DateTime.ParseExact(dateString, format, provider);
var str = "Sat Sep 22 13:15:03 2018";
var date = DateTime.ParseExact(str, "ddd MMM dd HH:mm:ss yyyy", CultureInfo.InvariantCulture);

Categories

Resources