I have the following code:
IFormatProvider culture = new System.Globalization.CultureInfo("es-ES", true);
date = DateTime.ParseExact(_date, "yyyy-MM-dd hh:mm", culture);
for _date = "2012-11-17 15:00"
it throws an exception
but for _date = "2012-11-17 10:00" works
Anyone can tell me what I'm doing wrong?
use HH instead of hh
date = DateTime.ParseExact(_date, "yyyy-MM-dd HH:mm", culture);
HH is for 24-hr
hh is for 12-hr
Related
I'm trying to convert current date to a specified format.
DateTime date = DateTime.ParseExact(DateTime.Now.ToString(), "yyyy-MM-dd HH:mm:ss.fff",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
I'm receiving the following exception.
String was not recognized as a valid DateTime.
My local TimeZone is (UTC+10:00)Melbourne.
What am I doing wrong here?
Your code (even if it worked), would do nothing. It would simply serialize and deserialize the date. I believe you're looking for this:
string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
It doesn't work because DateTime.Now.ToString() is giving a string like (I happen to be in the same timezone, and presumably have the same culture as you):
14/01/2016 3:54:01 PM
Which is of the format:
dd/MM/yyyy h:mm:ss tt
Which does not match the format you're using: yyyy-MM-dd HH:mm:ss.fff
Try this:
string fm = "yyyy-MM-dd HH:mm:ss.fff";
string str = DateTime.Now.ToString(fm, CultureInfo.InvariantCulture);
DateTime dt = DateTime.ParseExact(str, fm, CultureInfo.InvariantCulture);
EDIT:
A better way to achieve the date in the format would be like
DateTime now = DateTime.Now;
CultureInfo culture = new CultureInfo("en-AU"); //Melbourne
Thread.CurrentThread.CurrentCulture = culture;
Console.WriteLine(now.ToString("yyyy-MM-ddTHH:mm:ss.fff"));
IDEONE DEMO
I try to parse a date providing a specified format with the following code :
var date = "30/06/2014";
var ret2 = DateTime.ParseExact(date, "dd/mm/yyyy", null);
var ret3 = DateTime.ParseExact(date, "dd/mm/yyyy", CultureInfo.CurrentCulture);
var ret4 = DateTime.ParseExact(date, "dd/mm/yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(ret2);
Console.WriteLine(ret3);
Console.WriteLine(ret4);
-----OUTPUT-----
30/01/2014 00:06:00
30/01/2014 00:06:00
30/01/2014 00:06:00
Could someone explain me why this code doesn't return the 30/06/2014 00:00:00 value I am expecting ?
mm is the placeholder for minutes. You should use MM for months
mm means minutes, not months. Try using MM instead.
As mentioned, mm represents Minutes - note that the minutes of each DateTime is 06:00 instead of the default 00:00, and 1 is the default for a month(since there is no 0 month).
See MSDN on Standard and Custom DateTime format strings for more info, as well as more helpful placeholders.
the code used the wrong format string
var ret2 = DateTime.ParseExact(date, "dd/mm/yyyy", null);
var ret3 = DateTime.ParseExact(date, "dd/mm/yyyy", CultureInfo.CurrentCulture);
var ret4 = DateTime.ParseExact(date, "dd/mm/yyyy", CultureInfo.InvariantCulture);
mm is placeholder for minutes.
to get month, use MM in the format string
var ret2 = DateTime.ParseExact(date, "dd/MM/yyyy", null);
var ret3 = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.CurrentCulture);
var ret4 = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);
check out the example on MSDN https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
I have below code
inputdatetime value is "2014/09/11 8:06 AM"
dateformat is "yyyy/MM/dd h:mm a"
CultureInfo culture = CultureInfo.InvariantCulture;
string dateforamat = string.Concat(date, " ", time);
returnValue = DateTime.ParseExact(inputDateTime, dateforamat, culture);
I am getting format exception
Use tt instead of a
DateTime.ParseExact("2014/09/11 8:06 AM", "yyyy/MM/dd h:mm tt", CultureInfo.InvariantCulture);
Read: The "tt" Custom Format Specifier
I have this String :
05/09/2013 23:23
And i want to convert it to DateTime with this:
DateTime alarmDateTime = new DateTime();
alarmDateTime = DateTime.ParseExact(date, "MM/dd/YYYY HH:mm", null);
And i get this Exception:
String was not recognized as a valid DateTime.
Any idea why it happens?
I think the year should be lower case 'y'. There is also no need to instantiate the date time on the first line as the value is overwritten on the second.
DateTime alarmDateTime = DateTime.ParseExact(date, "MM/dd/yyyy HH:mm", null);
If date seperator in your system is "/" then just changing YYYY to yyyy will work.
If it is not then use this
string date = "05/09/2013 23:23";
DateTime alarmDateTime = new DateTime();
alarmDateTime = DateTime.ParseExact(date, "MM/dd/yyyy HH:mm",
CultureInfo.InvariantCulture);
I have to convert a date string to a DateTime object as follows
tmpdate = "27-Apr 14:53";
TheDate = DateTime.ParseExact(tmpdate, "DD-MMM HH:mm", CultureInfo.InvariantCulture);
I keep getting exceptions about the string not being a valid date time. I've tried adding in the year as well with no luck. Any suggestions?
Try dd-MMM HH:mm - note lowercased dd.
try with little d :
TheDate = DateTime.ParseExact(tmpdate, "dd-MMM HH:mm", CultureInfo.InvariantCulture);
string tmpdate = "27-Apr 14:53";
DateTime TheDate = DateTime.ParseExact(tmpdate, "dd-MMM HH:mm", System.Globalization.CultureInfo.InvariantCulture);