Error in converting string to datetime in C#.net - c#-4.0

I am getting an error in converting string to date in C#.
The error is:
string is not valid datetime.
Below is the code that I am using to convert string into datetime.
string[] DateFormat = { "dd-MM-yyyy", "dd/MM/yyyy", "MM-dd-yyyy", "MM/dd/yyyy" ,"dd-MMM-yy"};
VendorSinceDate = DateTime.ParseExact(dtresult.Rows[0]["VendorSinceDate"].ToString(),DateFormat,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None);
//dtresult.Rows[0]["VendorSinceDate"].ToString()="06-Jun-12 12:00:00 AM";

None of your possible DateFormats include the time, but the string you're trying to convert does.
You need to add a matching DateFormat, e.g. dd-MMM-yy hh:mm:ss tt

Related

Convert string to date format in azure data factory using set variable

I have string date in set variable "20211222"
And I want to convert it into date like 2021-12-22.
And I have use this function in variable set dynamic content
#formatDateTime('20211222', 'yyyy-MM-dd')
But error occur In function 'formatDateTime', the value provided for date time string '20211222' was not valid. The datetime string must match ISO 8601 format
Is there any other function to convert this string "20211222" into date?
Actually the string '20211222' is already in the unambiguous format of YYYYMMDD and will always be interpreted this way. If you need to use this string input as a date, just do a cast:
SELECT CAST('20211222' AS date); -- 2021-12-22
If you wanted to formerly go from your text input YYYYMMDD to a text output of YYYY-MM-DD, then make a round trip:
SELECT CONVERT(varchar(10), CAST('20211222' AS date), 120);
Function formatDateTime expects "a string that contains the timestamp".
Example:
formatDateTime('03/15/2018 12:00:00', 'yyyy-MM-ddTHH:mm:ss')
You would have to manage to input in a timestamp format. The default format for the timestamp is "o" (yyyy-MM-ddTHH:mm:ss:fffffffK), which complies with ISO 8601 and preserves time zone information.
Please use the below logic:
#concat(substring(pipeline().parameters.Test,0,4 ),'-',substring(pipeline().parameters.Test,4,2),'-',substring(pipeline().parameters.Test,6,2))

Datetime parse exception in C#

I have tried all the ways, however not sure why the string date is not getting converted to datetime.
string windowsTime = "2/21/2009 10:35:14 PM"
DateTime time = DateTime.ParseExact(windowsTime, "MM/dd/yyyy hh:mm:ss tt", null);
I used, DateTime.Parse, ParseExact, Convert.ToDatetime.
But nothing is working, I am getting "String was not recognized as a valid DateTime.".
can somebody advise what am I doing wrong ?
Since the month has only one digit this is correct M.
You also have to use CultureInfo.InvariantCulture instead of null(means current-culture). Otherwise all / will be replaced with the actual date separator of your current culture( for me de-DE it's .):
DateTime time = DateTime.ParseExact(windowsTime, "M/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
Custom date and time format strings: the "/" Custom Format Specifier
as specified in the MSDN
"MM" The month, from 01 through 12. More information: The "MM" Custom
Format Specifier.
Custom Datetime

How to Convert Date with ZZZ

I'm want to query for an object in my CoreData using NSDate.
I have an string like "2013:32:17 06:32:24 -0300".
When I get the object and convert it using "[format setDateFormat:#"yyyy:mm:dd hh:mm:ss ZZZ"]" I get a string like "2013:32:17 09:32:24 +0000"
How do I convert to the same original format above (-0300 ???)
Tk's!!

Convert string to Datetime Exception for 15-07-2013

I am trying to convert string to datetime. But it is giving me exceptioncontibously.
Kindly help.
DateTime dt = Convert.ToDateTime("15-07-2013");
I am getting exception as "String was not recognized as a valid DateTime."
Now i have the string as "15-07-2013 07:12:00 PM"
When i am using the code as mentioned below i am getting exception.
DateTime dtCurrentFile = DateTime.ParseExact("15-07-2013 07:12:00 PM", "dd-MM-yyyy HH:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None);
I am getting the same exception.
Your string format is "dd-MM-yyyy" , but Convert.ToDateTime() default format is "MM-dd-yyyy" . So Options are:
Changing your string format to "07/15/2013"
Forcing the conversion to adapt with it using:
DateTime dt = DateTime.ParseExact("15-07-2013", "dd-MM-yyyy",CultureInfo.InvariantCulture, DateTimeStyles.None);
You can always force the format with DateTime.ParseExact and avoid culture issues with using InvariantCulture:
DateTime dt = DateTime.ParseExact("15-07-2013", "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None);
Custom Date and Time Format Strings
However, your code works for me with culture "de-DE".
Update:
You have to use lower hh for the hours when you provide the am/pm designator:
DateTime.ParseExact("15-07-2013 07:12:00 PM", "dd-MM-yyyy hh:mm:ss tt", CultureInfo.InvariantCulture, DateTimeStyles.None)
HH means 24h format which makes no sense at all with the AM/PM designator.
try to use ParseExact:
DateTime dt = DateTime.ParseExact("15-07-2013", "dd-MM-yyyy", null);
The problem why you are getting exception is that by default C# supports date time in this format
"MM/dd/yyyy" where as you are trying passing date time is this format "dd-MM-yyyy"
although you can convert using your format but for that you need to tell the compiler your date is in which format so for that you can use
DateTime
myDate = DateTime.ParseExact("15-07-2013", "dd-MM-yyyy",
System.Globalization.CultureInfo.InvariantCulture);

SharePoint 2010 DateTime Culture Format Error

I'm trying to pass a Datetime to SharePoint list. For german datetime format it works, but it needs to be variable so i tried this:
itemsList[i]["FTPDate"] = DateTime.ParseExact(entity.FTPDate.ToString("dd MMM yyyy"), "dd MMM yyyy", Culture);
but I still get an invalid data exception.
What am I doing wrong?
Sharepoint dates are saved according to ISO8061, there is a helper method on the SPUtility class that can convert your datetime to the correct string.
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.createiso8601datetimefromsystemdatetime.aspx

Resources