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);
Related
I am trying to get the date (& datetime) from the url and then convert it into its proper format before storing it in the db.
var reqDate = Request.QueryString["StartDate"];
//at this point I have reqDate: 05/15/2018 00:00:00
reqDate = reqDate.Substring(0, reqDate.IndexOf(" ") + 1);
//after stripping off the time part I have: 05/15/2018
timingRequest.ReqDate = DateTime.ParseExact(reqDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
//but this throws the exception
URL:
Same is the case with startDateTime
var reqDateTime = Request.QueryString["startDateTime"];
timingRequest.IgnoreEntry = DateTime.ParseExact(reqDateTime, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
In your first scenario, No need to add +1 after reading indexOf(" "). +1 adding extra space to date
//Lets take date in string is "05/15/2018 00:00:00"
Console.WriteLine(s.Substring(0, reqDate.IndexOf(" ")+1)); /*This will print "05/15/2018 " WITH EXTRA SPACE*/
Correct way is s.Substring(0, s.IndexOf(" "))
In second scenario, use date format like HH:mm:ss instead of HH:mm tt
//Here use "hh:mm:ss" instead of "hh:mm tt"
DateTime dateTime = DateTime.ParseExact(reqDateTime, "dd/MM/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
Elegant approach would be:
#Credit Stephen Muecke
After looking at your URL, you can write a method having parameters like,
public ActionResult Create(int empId, int attID, DateTime startDate, DateTime startDateTime)
{
/*Do your work here, DefaultModelBinder will take care of parameters*/
}
First Example fix:
var reqDate = Request.QueryString["StartDate"];
reqDate = reqDate.Substring(0, reqDate.IndexOf(" "));
timingRequest.ReqDate = DateTime.ParseExact(reqDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
Second Example Fix:
var reqDateTime = Request.QueryString["startDateTime"];
timingRequest.IgnoreEntry = DateTime.ParseExact(reqDateTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
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 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
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);