I have a string containing value in the format of 11/22/2019 00:00:00
I want to store this in a DateTime column.
I have tried this:
DateTime date ;
DateTime dtVal;
// string abcd contains 11/22/2019 00:00:00
abcd = date.ToString("MM/dd/yyyy hh:mm:ss", CultureInfo.InvariantCulture);
if (DateTime.TryParseExact(abcd, "MM/dd/yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out converteddate))
{
dtVal = converteddate; // I am getting 22/11/2019 12:00:00
}
I want the dtval value to hold the datetime value as - 11/22/2019 00:00:00
How to achieve this ?
Thanks
DateTime itself does not have its "natural" format, as John commented - it is internally "just a number" ;-) . The ToString() method depends on Culture you set.
Related
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);
I'm trying to parse a string that contains a month and a year ("April 2017" for example) into a DateTime:
var foo = DateTime.ParseExact(dateToParse, "MMMM yyyy", null, DateTimeStyles.None);
However the format of the result is 04/01/2017 00:00:00 instead of April 2017. Did I miss something?
"I'm trying to parse a string" and parsing your string to DateTime worked without a problem.
A DateTime has a value and no format, if you want to get again another sting which represents that DateTime in a specific format you have to use DateTime.ToString.
For example:
DateTime dt = DateTime.ParseExact("April 2017", "MMMM yyyy", null, DateTimeStyles.None);
string monthAndYear = dt.ToString("MMMM yyyy"); // same as your original string "April 2017"
04/01/2017 00:00:00 is the formatted output with default formatting of the value of the DateTime instance you parsed.
DateTime does not have an implicit format.
If you want to display it in the form you mentioned you need to do something like foo.ToString("MMMM yyyy")
I am getting the account expiry date of the employee from database in a string variable and the values which is in this string variable is in the format : 6/26/2016 9:14:03 AM. Now i want this format to be converted in dd-mmm-yyyy hh:mm:ss am/pm.How can i resolve this.
I tried the following code :
DateTime passexpiredate = DateTime.ParseExact(app_user.GetPasswordExpiry(empCode), "dd-MMM-yyy hh:mm tt", null);
lblPassExpiry.InnerText = "Password Expiry Date: "+passexpiredate.ToString();
Life is to easy using DateTime.ParseExact:
DateTime.ParseExact("12/02/21 10:56:09 AM", "yy/MM/dd HH:mm:ss tt", CultureInfo.InvariantCulture).ToString("dd-MMM-yyyy HH:mm:ss tt");
You've to pass the required date time format in ToString() method as mentioned below:
DateTime passexpiredate = DateTime.Parse(app_user.GetPasswordExpiry(empCode));
lblPassExpiry.InnerText = "Password Expiry Date: " + passexpiredate.ToString("dd-MMM-yyy hh:mm tt");
You need to make DateTime from string 6/26/2016 9:14:03 AM. And then format it to dd-mmm-yyyy hh:mm:ss am/pm
DateTime.ParseExact requires you to specify format exactly as it matches with string representation of datetime value. So that's not going to work for your case.
You will have to first convert string to DateTime instance and then format it while displaying.
string date = "6/26/2016 9:14:03 AM";
var dt = DateTime.Parse(date);
var dtStr = dt.ToString("dd-mmm-yyyy hh:mm:ss tt");
Console.WriteLine(dtStr); // Output: 26-14-2016 09:14:03 AM
I have a Nullable DateTime, and I got an error :
Additional information: String was not recognized as a valid DateTime.
I looked at here, here,here and also here . I also tried String.Format("{0:s}", dateTime), but it does not change my DateTime format.My code is like below,
if (person.JsonData.PasswordChangeRequestTime != null)
{
DateTime data;
data = DateTime.ParseExact(((DateTime)person.JsonData.PasswordChangeRequestTime).Date.ToStringDateTime(), "dd'-'MM'-'yyyy HH':'mm':'ss", CultureInfo.InvariantCulture);
person.setColumnValue("passwordchangerequesttime", data);
}
One of my DateTime is like this:
1/1/2015 2:00:00 PM
I want them in a format of
1-1-2015 14:00:00
what is wrong with my DateTime.ParseExact function?
By the way, I do not want to use subString function!
You don't need to do anything.
Your (DateTime)person.JsonData.PasswordChangeRequestTime already a DateTime, what you see this is probably in a debugger or something.
A DateTime does not have any implicit format. It just have date and time values. Format concept only matter when you get it's textual (string) representation which is usually done with DateTime.ToString() method.
If you wanna get exact string representation of it, you can use ToString method with proper format and culture settings like;
((DateTime)person.JsonData.PasswordChangeRequestTime)
.ToString("d/M/yyyy h:mm:ss tt", CultureInfo.InvariantCulture)
genereates 1/1/2015 2:00:00 PM and
((DateTime)person.JsonData.PasswordChangeRequestTime)
.ToString("d-M-yyyy HH:mm:ss", CultureInfo.InvariantCulture)
generates 1-1-2015 14:00:00 formatted strings.
If your 1/1/2015 2:00:00 PM is string, not a DateTime, you need to parse it to DateTime with proper format first then generate it's string representation with proper format as well.
string s = "1/1/2015 2:00:00 PM";
DateTime dt;
if(DateTime.TryParseExact(s, "d/M/yyyy h:mm:ss tt", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
dt.ToString("d-M-yyyy HH:mm:ss", CultureInfo.InvariantCulture);
// Generates 1-1-2015 14:00:00
}
I have a custom date format that I want to convert to Datetime so I can then insert into my database, I tried using Datetime.ParseExact() But I think I'm misunderstanding something as the code throws a System.FormatException.
I have the following date format from a csv
> 6/11/2014 9:00
and I wish to convert it to the mysql datetime format
> 0000-00-00 00:00:00 OR yyyy-MM-dd HH:mm:ss
Notice they haven't included the seconds in the original date so I am unsure (without appending them to the end) how to set all records to just have "00" for seconds as it is not available.
I tried the following which throws an exception
DateTime myDate = DateTime.ParseExact("6/11/2014 9:00", "yyyy-MM-dd HH:mm",
System.Globalization.CultureInfo.InvariantCulture);
first thing you need to convert string to date time and than convert datetime tos tring
string strd = "6/11/2014 9:00";
DateTime dt ;
//convert datetime string to datetime
if(DateTime.TryParse(strd, out dt))
{
//convert datetime to custom datetime format
Console.WriteLine("The current date and time: {0: yyyy-MM-dd HH:mm:ss}",
dt); ;
}
output
I know this is late to answer that but I'm really surprised none of answer consider to use IFormatProvider to prevent a possible parsing error because of / format specifier or considering your string is a standard date and time format for your CurrentCulture or not so you can or can't use DateTime.TryParse(string, out DateTime) overload directly.
First of all, let's look at what DateTime.ParseExact documentation says:
Converts the specified string representation of a date and time to its
DateTime equivalent. The format of the string representation must
match a specified format exactly or an exception is thrown.
In your case, they don't match. You should use d/MM/yyyy H:mm format to parse your example string with a culture that have / as a DateSeparator. I almost always suggest to use DateTime.TryParseExact method in this kind of situations;
string s = "6/11/2014 9:00";
DateTime dt;
if(DateTime.TryParseExact(s, "d/MM/yyyy H:mm", CultureInfo.InvariantCulture,
DateTimeStyles.None, out dt))
{
Console.WriteLine(dt.ToString("yyyy-MM-dd HH:mm:ss"));
// result will be 2014-11-06 09:00:00
}
If you know formats of your dates, then you can do this:
string stringDate = "6/11/2014 9:00";
//Your date formats of input
string[] dateFormats = new string[]
{
"d/MM/yyyy H:mm",
"dd/MM/yyyy H:mm",
"dd/MM/yyyy HH:mm",
"dd/MM/yyyy H:mm:ss",
"dd/MM/yyyy HH:mm:ss"
/* And other formats */
};
DateTime convertedDate;
bool isSuccessful = DateTime.TryParseExact(stringDate, dateFormats,
System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out convertedDate);
if (isSuccessful)
{
//If conversion was successful then you can print your date at any format you like
//because you have your date as DateTime object
Console.WriteLine(convertedDate.ToString("dd-MM-yyyy HH:mm:ss")); /* Or other format you want to print */
}
I hope it will be helpful to you.