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

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/

Related

How to convert the following string to date string again?

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

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

How can I convert this string to date?

Im making a post from a view and getting it in a actionresult as a string.
The value I get is:
Tue Feb 18 2014 00:00:00 GMT-0300 (Hora oficial do Brasil)
Using DateTime.Parse throws an exception:
String was not recognized as a valid DateTime.
What makes this string invalid, and how can I successfully convert it to a DateTime?
DateTime.Parse throws exception for this string because it does not have a standart date/time format.
If your GMT-0300 (Hora oficial do Brasil) is stable in your string, you can use;
var s = "Tue Feb 18 2014 00:00:00 GMT-0300 (Hora oficial do Brasil)";
var date = DateTime.ParseExact(s,
"ddd MMM dd yyyy HH:mm:ss 'GMT'K '(Hora oficial do Brasil)'",
CultureInfo.InvariantCulture);
Console.WriteLine(date);
Output will be;
2/18/2014 12:00:00 AM
Here is a demonstration.
I don't think there is a way to parse your (Hora oficial do Brasil) part except using string delimiter.
Take a look at;
The "K" Custom Format Specifier
I don't know why K specifier doesn't work on Ideone actually. I have to put -0300 part also as a string delimiter for generating example. It can be an issue with DateTimeKind enumeration but I'm not sure..
The string is invalid because of the 'GMT' and the '(Hora oficial do Brasil)' parts.
Simply put: the parser is unable to determine what is part of a date time and what is not.
By using format strings you will be able to parse the string into the DateTime format.
see: MSDN: Custom Date and Time Format Strings
in your case this format string will work: "ddd MMM dd yyyy hh:mm:ss 'GMT'K '(Hora oficial do Brasil)'".
You can use it like this:
string input = "Tue Feb 18 2014 00:00:00 GMT-0300 (Hora official do Brasil)";
string[] format = { "ddd MMM dd yyyy hh:mm:ss 'GMT'K '(Hora official do Brasil)'" };
DateTime date;
if (DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
//Yepee the input was parsed correct
}
else
{
//system was unable to parse the string
}
Or like this if error handling is not nessesary:
string input = "Tue Feb 18 2014 00:00:00 GMT-0300 (Hora official do Brasil)";
string format = "ddd MMM dd yyyy hh:mm:ss 'GMT'K '(Hora official do Brasil)'";
DateTime date = DateTime.ParseExact(input, format, CultureInfo.InvariantCulture);

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