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);
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 have a string like
string startdatestring = "2017-03-10T07:19:28.907";
DateTime firstdate = DateTime.ParseExact(startdatestring,
"yyyy/MM/dd hh:mm:ss",
CultureInfo.InvariantCulture);
var firstDateString = firstdate.ToString("yyyy-MM-dd");
want the output only 2017-03-10
Modify your code like below
string startdatestring = "2017-03-10T07:19:28.907";
//1st way - Convert.ToDatetime works properly
//DateTime firstdate = Convert.ToDateTime(startdatestring);
//2nd way - DateTime.Parse also works perfect
//DateTime firstdate = DateTime.Parse(startdatestring);
//3rd way - DateTime.ParseExact also works perfect
DateTime firstdate = DateTime.ParseExact(startdatestring,
"yyyy-MM-ddTHH:mm:ss.fff",
CultureInfo.InvariantCulture);
var firstDateString = firstdate.ToString("yyyy-MM-dd");
Console.WriteLine(firstDateString);
OUTPUT
2017-03-10
Your input string "2017-03-10T07:19:28.907" doesn't match the format string "yyyy/MM/dd hh:mm:ss".
Considering your input is already in a standard format, you can use DateTime.Parse:
var firstdate = DateTime.Parse(startdatestring);
DateTime firstdate = DateTime.ParseExact(startdatestring,
"yyyy-MM-dd hh:mm:ss",
CultureInfo.InvariantCulture);
it's yyyy-MM-dd instead of yyyy/MM/dd
;)
Change you code as below
DateTime firstdate = DateTime.ParseExact(startdatestring,
"yyyy-MM-ddTHH:mm:ss.fff",
CultureInfo.InvariantCulture);
Do something like this
string startdatestring = "2017-03-10T07:19:28.907";
string s = startdatestring.Substring(0, 9);
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 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 a string like this:
3/4/2013 or like this 11/10/2012. This is in mm/dd/yyyy format which I want to convert to MM/dd/yyy. I am doing like this:
DateTime publicationDate = DateTime.ParseExact(myDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
This is throwing me an error:
String was not recognized as a valid DateTime.
What is going on here?
EDIT:
After reviewing all the answers, I want to show Month and Day of the DateTime variable.
So I cannot do something like this:
string publicationDate = DateTime.ParseExact(myDate, "M/d/yyyy", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy");
Having a string will not solve my problem because I am using this variable to show only day and month.
When I tried parsing this 'publicationDate' back to DateTime is truncates the '0's from month and day.
Hope I made my point here.
Answered:
string publicationDate = DateTime.ParseExact(myDate, "M/d/yyyy", CultureInfo.InvariantCulture).ToString("MM/dd");
You should add leading zeroes to the month and day.
This way: 04/03/2013
var myDateTime = DateTime.ParseExact(
"03/04/2013",
"MM/dd/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
If leading zeroes are a problem, then do:
var myDateTime = DateTime.ParseExact(
"3/4/2013",
"M/d/yyyy",
System.Globalization.CultureInfo.InvariantCulture);
Finally, if you want to add leading zeroes:
string myFormattedDateTime = DateTime.ParseExact(
"3/4/2013",
"M/d/yyyy",
System.Globalization.CultureInfo.InvariantCulture)
.ToString("MM/dd/yyyy");
Use this pattern instead: "M/d/yyyy" to parse it and "MM/dd/yyyy" for ToString:
DateTime publicationDate = DateTime.ParseExact(dt, "M/d/yyyy", CultureInfo.InvariantCulture);
// if you want to display two digits for day and month:
Console.WriteLine(publicationDate.ToString("MM/dd/yyyy"));
It works for both as you can see here: http://ideone.com/M7luBD
Simple solution was this:
string publicationDate = DateTime.ParseExact(myDate, "M/d/yyyy", CultureInfo.InvariantCulture).ToString("MM/dd");
Thank you all for your answers!
Convert your string ToDateTime first. Make sure you have leading zeroes.
string date = "03/04/2013";
DateTime dt = Convert.ToDateTime(date);
[TestCase("3/4/2013", 3, 4, 2013)]
[TestCase("11/4/2013", 11, 4, 2013)]
public void DateTest(string date, int month, int day, int year)
{
var publicationDate = DateTime.ParseExact(date, "M/d/yyyy", CultureInfo.InvariantCulture);
Assert.AreEqual(day, publicationDate.Day);
Assert.AreEqual(month, publicationDate.Month);
Assert.AreEqual(year, publicationDate.Year);
}
Both test cases pass.
If your format asks for MM/dd/yyyy then you need to supply the string as such (04/03/2013 instead of 4/3/2013). So either
use M/d/yyyy if your supplied date is without leading zero's
or use the MM/dd/yyyy if you can supply leading zero's