string to DateTime(date) - c#

I am trying to parse a string i recieved from my webservice into a DateTime so i can look if the date of that datetime is today or not.
I looked a bit and found on msdn and stackoverflow that these possibilities should work, they do not work for me for some reason.
string starttime = obj.TIME; //time i get from webservice = "02/14/2017 00:00:00"
DateTime startTimeCon = DateTime.Parse(starttime);
DateTime startTimeCon2 = Convert.ToDateTime(starttime);
error:
The string is not recognised as a valid DateTime
Any ideas why?

It seems you have different culture in your system.
Use ParseExact() instead of Parse():
DateTime startTimeCon = DateTime.ParseExact(starttime,
"MM/dd/yyyy HH:mm:ss",
CultureInfo.InvariantCulture);
HH used for 24 hours, you can use hh for 12 hours
Also, you can set appropriate culture in Parse():
DateTime startTimeCon = DateTime.Parse(starttime, neededCulture);

Related

How can I convert this date "03/01/2018 12:00 AM" to "2018-03-01" in C#?

I am using this code to convert a date 03/01/2018 12:00 AM to 2018-03-01 in C#:
DateTime startDate = DateTime.ParseExact(TextBox1.Text.ToString(),
"yyyy-mm-dd",
System.Globalization.CultureInfo.InvariantCulture);
but it throws an exception
String was not recognized as valid datetime
This Will Work Like A Charm
string bs = "03/01/2018 12:00 AM";
String startDate = DateTime.ParseExact(bs,"MM/dd/yyyy hh:mm tt",System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy-MM-dd");
Console.WriteLine(startDate);
// Outputs 2018-03-01
You can verify the code here
A DateTime represents a particular point in time. ParseExact is a way of turning a string into a DateTime. By saying ParseExact with "yyyy-mm-dd" you are telling it that the string you are giving it begins with a four digit year, which it doesn't. Fix the format string that you are supplying so that the parse works.
Once you have the value in your DateTime variable, you can use ToString("yyyy-mm-dd") to turn that DateTime back into a string.
Remove AM from your Textbox and edit format string, then your sample code will work.
This line run successfully :
DateTime startDate = DateTime.ParseExact("03/01/2018 12:00", "MM/dd/yyyy hh:mm", System.Globalization.CultureInfo.InvariantCulture);
string inputDate = "03/01/2018 12:00 AM";
string outputDate = DateTime.Parse(inputDate).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);

Parse string to DateType ASP.NET C# exception - The string was not recognized as a valid DateTime

So I have a date which is in this format. My goal is to add 7 days to this string startdate and post it into a database as a string. However, I have to convert it to datetime to allow me to add days to it. I am reading startdate from a database but this is what it looks like.
string startdate = "10-03-2018 03:15PM";
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime starttime2 = DateTime.ParseExact(startdate, "MM/dd/yyyy HH:mm tt", culture);
// It is breaking on the above line with the error - The string was not recognized as a valid DateTime.
DateTime endtime2 = starttime2.AddDays(+7);
Anyone able to help me solve this issue? I am new to C# and would appreciate any help at all..
Thank you
You have specified wrong format actually. You should be specifying the following format:
"dd-MM-yyyy hh:mmtt"
as your date is in format :
"10-03-2018 03:15PM"
Assuming that the first number us for day and second is for month, otherwise you can swap those.
You can see more details on the usage of ParseExact here.
Try this:
string startdate = "10-03-2018 03:15PM";
IFormatProvider culture = new CultureInfo("en-US", true);
DateTime starttime2 = DateTime.ParseExact(startdate, "dd-MM-yyyy hh:mmtt", culture);
no space between mm and tt. also this is 12 hours format so hh
I think this will help you
public static DateTime AddDaysToMyDate(string date)
{
return DateTime.ParseExact(date, "dd-MM-yyyy hh:mmtt", new CultureInfo("en-US", true)).AddDays(7);
}
Use it as
DateTime newDateTime = AddDaysToMyDate("10-03-2018 03:15PM");

DateTime.ParseExact() - DateTime pattern 'y' appears more than once with different values

I have spent a day trying to get DateTime.ParseExact() to work based on this correctly answered question at Parse string to DateTime in C# however, I cannot get the answer to work.
Here is my code:
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd H:mm:ss.yyy";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);
Error:
DateTime pattern 'y' appears more than once with different values.
Note: error reported in original version of the post does not show up in this sample, but may be related:
"When converting a string to DateTime, parse the string before putting each variable into the DateTime object."
Your format should be yyyy-MM-dd HH:mm:ss.fff
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd HH:mm:ss.fff";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);
See: Custom Date and Time Format Strings
The error I get with that code is the following:
DateTime pattern 'y' appears more than once with different values.
It's pretty self-explanatory. Looking at the docs, you need to use .fff here:
"yyyy-MM-dd H:mm:ss.fff"
yyy is: The year, with a minimum of three digits, but since you already have yyyy in your pattern, you get the duplicate specifier error.
Your format is wrong, you used y twice.
string testDateRaw = #"2014-05-21 10:08:15.965";
string format = "yyyy-MM-dd H:mm:ss.fff";
DateTime testDate = DateTime.ParseExact(testDateRaw, format, CultureInfo.InvariantCulture);
System.Console.WriteLine(testDate);

DateTime.ParseExact strings to datetime

I'm about to begin cursing at my computer!
I have one program that output a datetime as a string, but I want to feed it into another as a datetime.
The string I get is on the form:
dd/MM/yy hh:mm:ss
And I would like to find an appropriate way to get a DateTime object back.
I'm thinking something like:
string date = "11/07/14 18:19:20";
string dateformat = "dd/MM/yy hh:mm:ss";
DateTime converted_date = DateTime.ParseExact(date,
dateformat, CultureInfo.InvariantCulture);
But several of the conversion of dates result in an Exception being thrown back with the message "Not valid timedate".
What am I missing?
'hh' for hour is actually 12 hour clock, 01-12. I think you want 'HH' or 'H' for 24-hour clock ('HH' is zero-padded, 'H' is not). Check out: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx for specific formats.
The hour is not in 12-hour format. For 24-hour format, it's H.
string date = "11/07/14 18:19:20";
string dateformat = "dd/MM/yy H:mm:ss";
DateTime converted_date = DateTime.ParseExact(date,
dateformat, CultureInfo.InvariantCulture);

DateTime object in a specific format irrespective of the input

I have a ASP.NET MVC app that provides the user to pick a date which is stored into the ViewModel.
This is the code that converts to date object:
viewModel.startDateAndTime = Convert.ToDateTime(buyToday.DealStartDateAndTime);
One of the developers has his system date time set to this format:
24-Feb-2014
On that system he's getting FormatException.
I would like to set the date time to use this format:
mm/dd/yyyy
not matter what the setting is on any system..
Tried using this piece of code which does'nt work:
string startDate = "24-Feb-2014";
DateTime startDateTime = DateTime.ParseExact(startDate, "mmddyyyy", CultureInfo.InvariantCulture);
Any clues are appreciated.
Thanks & Regards.
Your input string does not match parsing pattern.
"24-Feb-2014" is much different then mmddyyyy, isn't it?
You can use DateTime.Parse with CultureInfo.InvariantCulture:
string startDate = "24-Feb-2014";
DateTime startDateTime = DateTime.Parse(startDate, CultureInfo.InvariantCulture);
otherwise, with ParseExact the input has to exactly match pattern, so you should pass 24022014 as input. But, just so you know, mm means minutes. For month, use MM :) So pattern should be ddMMyyyy. Check Custom Date and Time Format Strings page on MSDN.
try this:
string startDate = "24-Feb-2014";
DateTime startDateTime = DateTime.ParseExact(startDate, "dd-MMM-yyyy",System.Globalization.CultureInfo.InvariantCulture);
mm returns The minute, from 00 through 59. So use MM
The month, from 01 through 12.
string startDate = "24-Feb-2014";
DateTime startDateTime = DateTime.ParseExact(startDate, "ddMMyyyy", CultureInfo.InvariantCulture);
Custom Date and Time Format Strings

Categories