This question already has an answer here:
Parsing ISO 8601 with timezone to .NET datetime
(1 answer)
Closed 9 years ago.
I want to cast in C# a String to DateTime.
My String contains: String input = "2012-07-31T00:00:00.000+0200"
and i used the following pattern: String datePattern = "yyyy-MM-dd%HH:mm:ss.fffz";
MyDateTime myDate = new DateTime();
MyDateTime myDate = DateTime.ParseExact(input, datePattern, null);
And i am getting the following error: String was not recognized as a valid DateTime.
Was pretty sure, because i am not sure how to solve this 'T'and which Timezone i should use.
There are three variants of it at the msdn side.
Which one i need to use, or can i create my own one?
Any suggestions?
You can use The "K" Custom Format Specifier with "yyyy-MM-ddTHH:mm:ss.FFFK" format like;
string s = "2012-07-31T00:00:00.000+0200";
var date = DateTime.ParseExact(s, "yyyy-MM-dd'T'HH:mm:ss.FFFK", CultureInfo.InvariantCulture);
Console.WriteLine(date);
Output will be;
7/30/2012 10:00:00 PM
Here a demonstration.
For more information take a look at;
Custom Date and Time Format Strings
Related
This question already has answers here:
Convert string to DateTime in c#
(3 answers)
Closed 3 years ago.
How do I convert string to date time? I am getting "2019-06-07T02" as string. I want to format string to date like below examples
Ex: 2019-06-07T02 -- Friday January 07,2019 2:00 AM
Ex: 2019-06-07T14 --- Friday January 07,2019 2:00 PM
var input = "2019-06-07T14";
var datetime = DateTime.ParseExact(input, "yyyy-MM-dd'T'HH", CultureInfo.InvariantCulture);
var output = datetime.ToString("dddd MMMM dd, yyyy h':'mm tt");
This does exactly what you need. (Source Docs)
Edit: I know it might be too manual but if you want to configure it more, this way you can, if not, go with oleksa's answer.
var str = "2019-06-07T02";
var dt = DateTime.ParseExact(str, "yyyy-MM-ddThh", CultureInfo.InvariantCulture);
var longstr = dt.ToLongDateString() + dt.ToLongTimeString();
please note that ToLongDateString and ToLongTimeString depends on windows user regional settings
the documentation
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/
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");
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");
}
This question already has answers here:
CONVERT MM/DD/YYYY HH:MI:SS AM/PM to DD/MM/YYYY in C#
(3 answers)
Closed 8 years ago.
I want to convert the string "2015/07/05" to the format 08-MAR-2015.
The code below keeps getting detected as an invalid datetime format (i.e the else statement below)
C# Code
string format = "dd-MMM-yyyy";
string dateString = "2015/07/05";
DateTime dateTime;
if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,DateTimeStyles.None, out dateTime))
{
MessageBox.Show(Convert.ToString(dateTime));
}
else // Invalid datetime format
{
MessageBox.Show("UBD date is not a valid date format: " + dateTime.ToString());
}
String formatting is performed by the String.Format method, not Convert.ToString. The Convert methods try to convert one type to another using the current culture's default format where required.
Try the following
String.Format(CultureInfo.InvariantCulture,"dd-MMM-yyyy",someDate);
This will ensure that the English month names will be used.
In non-English cultures the following line will return the local month name
String.Format("dd-MMM-yyyy",someDate);