I'm trying to convert a date in yyyymmdd format to yyyy-mm-dd with the following code:
tdrDate = DateTime.ParseExact(dateString, "yyyymmdd", null).ToString("yyyy-MM-dd");
This works the only problem is that when I have a date such as this "20070205" I get back "2007-01-05". I don't know why this is happening, any help is appreciated.
tdrDate = DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");
You need MM, not mm. mm is for minutes.
It should be:
DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");
Capital 'MM' in the first date format string.
"yyyymmdd" must be "yyyyMMdd".
mm is for minutes.
Try this :
tdrDate = DateTime.ParseExact(dateString, "yyyyMMdd", null).ToString("yyyy-MM-dd");
use MM instead mm,
mm is for minute &
MM is for Month that is why it is taking 01 (default value of MM).
The format string is case-sensitive, so "mm" is different to "MM". You are parsing minutes ("mm"), which is why the value of months ("MM") is always at the default value of 1.
try this:
DateTime.ParseExact("20070205", "yyyyMMdd", null).ToString("yyyy-MM-dd")
Disclaimer I know nothing about C#'s date formatting.
But I'm guessing the problem is that you used mm in the first format string, and MM in the second.
A handy reference: SteveX Compiled: String formatting in C#
Here you're parsing the date to create a date object, formatting the date object to a string, and discarding the date object. That sounds like more work than simple string processing:
tdrDate = dateString.Substring(0,4) + '-' +
dateString.Substring(4,2) + '-' +
dateString.Substring(6,2);
Unless you need the validation that's performed by DateTime.ParseExact(), which will throw a System.FormatException if given an invalid date, I'd probably just use the string formatting approach.
Related
I am trying to parse the date by using below code
DateTime mydate = DateTime.ParseExact(datetoconvert,"dd/mm/yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
but its output is wrong, the datetoconvert in above code is 30/Mar/2017 but output is 29/Jan/2017
looking forward for your valuable answers...
Lowercase mm means minute, use MM
DateTime mydate = DateTime.ParseExact(datetoconvert,"dd/MM/yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
If you want to output it as 30/Mar/2017(different topic):
string result = mydate.ToString("dd/MMM/yyyy", CultureInfo.InvariantCulture);
But note that / has a special meaning too(in Parse and ToString). It will be replaced with your current cultures date-separator which seems to be / but fails with a different. You can avoid it by specifying CultureInfo.InvariantCulture or by masking it by wrapping it with apostrophes:
DateTime mydate = DateTime.ParseExact(datetoconvert,"dd'/'MM'/'yyyy",System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat);
replace
"dd/mm/yyyy"
with
"dd/MMM/yyyy"
because "Jan" is matched by MMM instead of mm (for minutes)
Reference
"MMM" The abbreviated name of the month.
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
The date format is wrong. try "dd/MM/yyyy" instead of "dd/mm/yyyy"
If you need abbrivated month name, use "dd/MMM/yyyy"
I tried converting 9/29/2013 2:44:28 PM (mm/dd/yyyy) to dd/mm/yyyy format.
I got a strange Date after Converting.
I tried
dateTimeVar.ToString("dd/mm/yyyy");
29/44/2013
The Date was a type of DateTime itself.
Lowercase mm means minutes, try this instead:
dateTimeVar.ToString("dd/MM/yyyy");
However, if this works depends on your local culture. If your current culture's date separator is different, / will be replaced with that. So if you want to enforce it use CultureInfo.InvariantCulture:
dateTimeVar.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
MM is for months, mm is for minutes. That's why it gets your minutes (which is 44) instead of your month value.
Use it like;
dateTimeVar.ToString("dd/MM/yyyy");
Check out;
The "MM" Custom Format Specifier
The "mm" Custom Format Specifier
And remember, / has special meaning when you use it as a date separator. It replace itself with your current culture date separator. Forcing to use with InvariantCulture would be better.
dateTimeVar.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Take a look at;
The "/" Custom Format Specifier
What if I want to convert a string in dd/MM/yyyy to DateTime?
Then you can use DateTime.ParseExact method.
Converts the specified string representation of a date and time to its
DateTime equivalent using the specified format and culture-specific
format information. The format of the string representation must match
the specified format exactly.
As an example;
string s = "01/01/2013";
DateTime dt = DateTime.ParseExact(s, "dd/MM/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);
Output will be;
1/1/2013 12:00:00 AM
Here a DEMO.
dateTimeVar.ToString("dd/mm/yyyy"); // Change to dd/MM/yyyy
The problem is mm stands for minute and you need MM which would be months
Tim's answer is correct, but to remove the format string altogether you can use. 'ToShortDateString'
DateTime date = DateTime.Today;
var stringDate = date.ToShortDateString();
var stringDate2 = date.ToString("dd/MM/yyyy");
I have a string "11 Jan 2011" which I want to convert to the datatype date (i.e 11 Jan 2011).
I have tried all resources about datetime.parse, datetime.parse exact but all these things gives me the same output 2011/01/11 12:00:00 AM. I really don't understand this behaviour. I tried the following:
1.DateTime date = DateTime.Parse("11 Jan 2011");
2.DateTime date = DateTime.ParseExact("11 Jan 2011" , #"dd MMM yyyy", System.Globalization.CultureInfo.InvariantCulture);
parsing and displaying are not the same thing
you parse the original string to a DateTime object but display results using Date/Time format strings
Both your calls are correct.
A DateTime structure preserves no information about formatting; it just represents the raw date and time.
What you need to do is ensure that when you display your date, you do so in the correct format - e.g. by calling string displayString = date.ToString("dd MMM yyyy");
Below code snippet showing "07/01/2011" instead of "07/09/2011". Anything wrong with this code snippet?
Code Snippet:
DateTime result;
DateTime.TryParseExact(
"07/09/2011",
"dd-mm-yyyy",
new CultureInfo("en-GB"),
System.Globalization.DateTimeStyles.None,
out result);
// shows "07/01/2011"
MessageBox.Show(result.ToString());
mm is "Minutes". MM is month. Also, it shouldn't match anything, as in your date you're using / to separate the components and in the pattern you`re using dashes.
So either your date pattern should be dd/MM/yyyy or your date string should be like 07-09-2011.
The correct format string is dd/MM/yyyy
dd-mm-yyyy should be dd/MM/yyyy because mm stands for minutes and - does not equal / in TryParseExact.
Check: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
i need to format date like "2010-04-21 11:35:22.440". can anyone help me?
the problem is i am seeing either 2009-06-15T13:45:30.0900000 or 2008-03-09 16:05:07Z but not the one i am looking for. thanks.
string formattedDate = dateTime.ToString("yyyy-MM-dd HH\\:mm\\:ss.fff");
Note that the case of MM and HH are important, MM is months, mm is minutes, and HH is 24h, vs hh being 12h.
Also notice the time separator is specified as \:, if you just use : it will use the time separator specified in your regional settings, which may not necessarily be a colon.
Custom Date and Time Format Strings link should point you in the right direction.
Use the format "yyyy-MM-dd hh:mm:ss.fff" with the ToString of the date variable.
e.g:
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));