String was not recognized as a valid datetime - Datetime conversion issue [duplicate] - c#

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 5 years ago.
I am having a date like "19/01/2018 15:30" as string.
I need to convert this to a DateTime object.
I am getting error:
String was not recognized as a valid datetime
This is my code:
DateTime drawDatetime = DateTime.ParseExact("19/01/2018 15:30"
, "dd/MM/yyyy hh:mm:ss", null);

You format string contains seconds but the date doesn't. Try this instead:
DateTime drawDatetime = DateTime.ParseExact(
"19/01/2018 15:30",
"dd/MM/yyyy HH:mm", null);

Related

Converting string that is dd.mm.yyyy. HH:mm:ss format to datetime C# [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 3 years ago.
So I have string that is 24.08.2010. 21:21:21, I want to convert it to 2010-08-24 21:21:21 to be able to save it in the db.
I tried this
var input = "22.08.2010. 7:00:00";
var date = DateTime.ParseExact(input,"yy-MM-dd HH:mm:ss.fff", null);
Console.WriteLine(date);
but I get error:
System.FormatException: String was not recognized as a valid DateTime.
Anyone have idea how to convert this?
First, you need to convert string input into date:
var input = "22.08.2010. 7:00:00";
var date = DateTime.ParseExact(input, "dd.MM.yyyy. H:mm:ss", null);
And than convert date to string:
Console.WriteLine(date.ToString("yy-MM-dd HH:mm:ss"));

Get PM or AM from string date C# [duplicate]

This question already has answers here:
How do I format a DateTime in a different format?
(2 answers)
Closed 4 years ago.
I have this string as a date and I want to add the AM/PM to the end.
From
10-17-2018 00:00:00
To
10-17-2018 00:00:00 AM
How is possible?
Thanks a lot!
So convert the string date into a real DateTime, then ToString() it back out in the format you like:
var dateStr = "10-17-2018 00:00:00";
var date = DateTime.Parse(dateStr);
Console.WriteLine(date.ToString("MM-dd-yyyy HH:mm:ss tt"));
Output: 10-17-2018 00:00:00 AM
DateTime formats: http://www.csharp-examples.net/string-format-datetime/

how to change date format to mm/dd/yyyy to dd/mm/yyyy in c# [duplicate]

This question already has answers here:
How to change date format from DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD?
(4 answers)
Closed 5 years ago.
im gettin the date in this format "3/16/2017 10:53:44 PM", how to get date in this format "16/3/2017 10:53:44 PM"
sample data
"timestamp":"2017-03-16T17:23:44.860Z"
desired op
"16/3/2017 10:53:44 PM"
code
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(bin.timestamp), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
data type
public String UpdatedTime { get; set; }
Or Try to parse the datetime.
For example
DateTime dateformat = DateTime.ParseExact(Convert.ToDateTime(bin.timestamp), "dd/M/yyyy h:mm:ss tt",CultureInfo.InvariantCulture);
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(dateformat,TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
You need to convert it to string:
binModel.UpdatedTime = bin.timestamp.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture));
For more information: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
A DateTime has no display format.
Only string represnetations of DateTime does.
You can specify the desired format as a parameter in one of the the ToString method overloads of the DateTime struct:
var s = DateTime.Now.ToString("dd/M/yyyy hh:mm:ss tt");
Now s contains the folowing string: "23/3/2017 09:01:32 AM" (my local time)
var currentDate = DateTime.Now.ToString("dd/MM/yyyy");

How to convert date format in c#? [duplicate]

This question already has answers here:
convert datetime to date format dd/mm/yyyy
(15 answers)
Closed 6 years ago.
I am getting data from XML in format of MM/dd/yyyy HH:mm:ss tt (e.g 7/21/2016 5:43:03 PM) but I want to convert it into date format of only dd/MM/yyyy (e.g 21/7/2016)
You could try something like this:
var dt = DateTime.ParseExact(input, "M/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture)
.ToString("dd/MM/yyyy");
Initially, using the ParseExact you create a DateTime object based on your input and later using the ToString you create the string representation of your DateTime in the specified format.
For info about the DateTime.ParseExact have a look here.
You can do this as a quick trick:
string date = "7/21/2016 5:43:03 PM";
var dateOnly = DateTime.Parse(date).ToString("MM/dd/yyyy");
Explanation:
DateTime.Parse(date) <--- Converts the string to DateTime object.
ToString("MM/dd/yyyy") <--- Converts the DateTime Object to the specified format.
string date = "7/21/2016 5:43:03 PM";//Date from XML
DateTime dateTime;
if (DateTime.TryParse(date, out dateTime))
{
var dateOnly = dateTime.ToString("dddd, MMMM dd, yyyy");
}

Need to convert string to DateTime format in C# [duplicate]

This question already has answers here:
Convert a string to a date in .net
(6 answers)
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 6 years ago.
I have a string stored in table "13/12/1985 12:00:00 a.m." when i tried to convert this to Datetime, i am getting an exception saying "String is not a valid DateTime Format". It is because the first part of the string(13) is month. Is there any way to convert the above string to (mm/dd/yyyy hh:mm:ss am/pm) format?. Actually the string saved in table is in the format of "dd/mm/yyyy". I want to convert to "mm/dd/yyyy" in Datetime
Try this, its convert dd/MM/yyyy hh:mm:ss tt format datetime to MM/dd/yyyy hh:mm:ss tt format
DateTime dt = DateTime.ParseExact("26/04/2016 12:00:00 PM", "dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture);
string newdate = dt.ToString("MM/dd/yyyy hh:mm:ss tt")

Categories

Resources