Convert string "2017-01-01T12:00:33:123456Z+02" to DateTime [duplicate] - c#

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 6 years ago.
i am trying to convert this string i get from the customer to a datetime, to store it in my db.
i tried several approches but nothing seems to work.
i need the offset in UTC time so i guess i need to do something with zz.
This is working:
string test = "2008-06-11T16:11:20.0904778Z";
DateTime publishingTimeStamp = DateTime.ParseExact(test, "o", CultureInfo.InvariantCulture,
DateTimeStyles.None);
But the string by the customer is slightly different, so i tried this one:
string test = "2017-01-01T12:00:33:123456Z+02";
DateTime publishingTimeStamp = DateTime.ParseExact(test, "yyyy-MM-dd'T'HH:mm:ss:ffffffzz", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None);
Unfortunaly it is not working. How can i convert the exact string "2016-01-01T10:00:55:123456Z+02"? Is it even possible because it uses ":" in front of ffffff
Thank you

Looks like you forget to escape Z as a string literal delimiter in your format as 'Z'.
string test = "2017-01-01T12:00:33:123456Z+02";
DateTime publishingTimeStamp = DateTime.ParseExact(test,
"yyyy-MM-dd'T'HH:mm:ss:ffffff'Z'zz",
CultureInfo.InvariantCulture, DateTimeStyles.None);
Console.WriteLine(publishingTimeStamp); // 01.01.2017 12:00:33
But since your string has UTC Offset value, I would parse it to DateTimeOffset instead.
string test = "2017-01-01T12:00:33:123456Z+02";
DateTimeOffset publishingTimeStamp = DateTimeOffset.ParseExact(test,
"yyyy-MM-dd'T'HH:mm:ss:ffffff'Z'zz",
CultureInfo.InvariantCulture,
DateTimeStyles.None);
Now you have a DateTimeOffset as {01.01.2017 12:00:33 +02:00}

Related

How convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second [duplicate]

This question already has answers here:
How to remove time portion of date in C# in DateTime object only?
(43 answers)
Closed 3 years ago.
I am doing an asp.net mvc project and I need to convert the string of "06/22/2019 00:00:00" to a valid DateTime type in format of 2019/06/22 without the part of hour and minute and second
You can use DateTime.ParseExact, here is an example :
http://net-informations.com/q/faq/stringdate.html
Finally, it should look like this :
string s = "06/22/2019 00:00:00";
DateTime myDate = DateTime.ParseExact(s, "MM/dd/yyyy HH:mm:ss",System.Globalization.CultureInfo.InvariantCulture);
Debug.WriteLine(myDate.ToString("MM/dd/yyyy"));
You can do this:
var dateString = "06/22/2019 00:00:00";
var datePart = dateString.Split(' ')[0];
var date = DateTime.Parse(datePart);
Though remember that DateTime will still have a default value for the time (12:00 AM), if you want the Date part only from the object, use date.Date which will return an instance with the default time (mentioned earlier).
DateTime contains default Time even if you access DateTime.Date. You can achieve format of date by converting Date into string.
Something like,
DateTime myDate = DateTime.ParseExact("06/22/2019 00:00:00", "MM/dd/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
string dateInFormat = $"{myDate.Year}/{myDate.Month}/{myDate.Day}";
POC : .net Fiddle
You convert the string to a DateTime object and then to display just the date portion you can use ToShortDateString like this:
var myDateTime = DateTime.Parse( "06/22/2019 00:00:00") //presumably use a variable here instead.
var date = myDateTime.ToShortDateString();
How you want to display this can be done using the CultureInfo part as shown here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.toshortdatestring?view=netframework-4.8

To change Date Format to "YYYYMMDD" [duplicate]

This question already has answers here:
Change format of DateTime object in c# and store back as DateTime object
(1 answer)
Set DateTime format
(6 answers)
Closed 3 years ago.
I have DateTime which contains standard Date Time. I am trying to convert this into "yyyymmdd" format.
DateTime deliveryDate = myRepository.DeliveryDate; //3/31/2018 12:00:00 AM;
DateTime myDeliveryDate = DateTime.ParseExact(deliveryDate.ToString(),
"yyyymmdd",
CultureInfo.InvariantCulture,
DateTimeStyles.None)
What is wrong in the above code, I got an error like this:
String was not recognized as a valid DateTime.at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
The DateTime class doesn't store the date in a particular format, if you want it represented like yyyymmdd you have to convert it to a string:
string myDeliveryDate = deliveryDate.ToString("yyyyMMdd");
You should note that month is a capital M (small m is for minutes).
DateTime is just the container of all the information. If you want it parsed in yyyyMMdd format, use
string myDeliveryDate = deliveryDate.ToString("yyyyMMdd");

Convert string to DateTime with format "dd/MM/yyyy" [duplicate]

This question already has answers here:
Convert string to DateTime in c#
(3 answers)
Closed 6 years ago.
I have a string that is a date in the format of "MMddyyyy". I need to convert it to a DateTime with formatting "dd/MM/yyyy". I created the following code, but I keep getting a Format Exception. Parse, ParseExact, TryParseExact all give a Format Exception also.
string dater = "10312016";
DateTime condate = Convert.ToDateTime(dater);
I have tried the following:
string dater = "10312016";
DateTime condate = DateTime.ParseExact(dater, "MMddyyyy", CultureInfo.InvariantCulture);
DateTime condate2 = DateTime.ParseExact(dater, "yyyyMMdd", CultureInfo.InvariantCulture);
condate works, but it is in the wrong format. It needs to be "dd/MM/yyyy"
string dater = "10312016";
DateTime condate = Convert.ToDateTime(dater).tostring("dd/MM/yyyy");
Just try this. I am posting answer from my mobile. So tostring syntax may not right. When u type tostring in IDE then it will be alright.
Edit
sorry for later answer. see this code and it is working.
string dater = "10312016";
DateTime condate = DateTime.ParseExact(dater, "MMddyyyy", System.Globalization.CultureInfo.InvariantCulture);
string strDate = condate.ToString("dd/MM/yyyy");
Th
anks

how to display only date from DateTime [duplicate]

This question already has answers here:
Display only date and no time
(11 answers)
Closed 8 years ago.
i m getting date from sql server database and it is displaying datetime as 1/15/2015 12:00:00 AM.
code in c# i used is:
dob_lbl.Text = reader[6].ToString();
//this is extracted using Sqlconnection so it's in array format.
i need only date to display.pls help.
try this:
string strDate = reader[6].ToString();
dob_lbl.Text = DateTime.ParseExact(strDate, "M/dd/yyyy hh:mm:ss tt",
CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");
The ToShortDateString method of DateTime should help with this.
Use it like:
string strDate = reader[6].ToString();
DateTime dateTime = DateTime.Parse(strDate);
string justDateStr = dateTime.ToShortDateString();
You could simply split the string and use the first part of the resulting array.
Something like:
dob_lbl.Text = reader[6].ToString().Split()[0];

Datetime value to string by replacing c# [duplicate]

This question already has answers here:
extract the date part from DateTime in C# [duplicate]
(5 answers)
Closed 8 years ago.
I have a datetime value below,
23/07/2014 04:15:00
How can i get date as below string value
23/07/2014
How can i get hour as below string value
04:15 AM/PM (depends hour time)
Any help will be appreciated.
Thanks.
I'd parse the string to DateTime first to avoid string manipulation.
var str = "23/07/2014 04:15:00";
var dt = DateTime.ParseExact(str, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
var date = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);//23/07/2014
var time = dt.ToString("hh:mm tt", CultureInfo.InvariantCulture);//04:15 AM
If you have the value as DateTime you can skip ParseExact part.
I'm not sure if you really use a DateTime instead of a string, but you should. If not, you can use DateTime.Parse/DateTime.TryParseExact to get a DateTime from a string.
DateTime has a method ToShortDateString
string dateOnly = dt.ToShortDateString();
// or to force / as separator even if current culture has different separator
dateOnly = dt.ToString("d", CultureInfo.InvariantCulture);
// hour+minute and AM/PM designator:
string hour = DateTime.Now.ToString("hh:mm tt", CultureInfo.InvariantCulture);
I strongly feel like taking a risk to answer your question but.. anyway.
How can i get date as below string value
DateTime dt = new DateTime(2014, 7, 23, 4, 15, 0);
Console.WriteLine(dt.ToString(#"dd\/MM\/yyyy")); // 23/07/2014
I escaped / character because it has a special meaning in custom date and time format strings. It means as; replace me with the current culture date separator. Take a look "/" Custom Format Specifier for more information.
How can i get hour as below string value
Console.WriteLine(dt.ToString("HH:mm tt", CultureInfo.InvariantCulture));
Output will be;
04:15 AM

Categories