Remove time from time and date - c#

I have a variable openDate which holds date and time, and I would like to strip just the date. I tried the below example and it is not working. What am I doing wrong, or rather how should I do it because the variable openDate remains the same even after trying to strip just the date? The value of openDate is "2012-03-08 00:00:00"
openDate = ! string.IsNullOrEmpty(node.ChildNodes[f].Attributes["ows_PMO_x0020_Origination_x0020_Date"].Value)
? node.ChildNodes[f].Attributes["ows_PMO_x0020_Origination_x0020_Date"].Value
: "" ;
openDate = String.Format("{0:MM/dd/yyyy}", openDate);

considering openDate is of a String type, i would do this
var dt = DateTime.Parse(openDate).ToString("MM/dd/yyyy");

From your code it is clear that openDate is of type string and you have value that is a string representation of DateTime, you can apply DateTime formatting on string values.
You have multiple options.
Convert string openDate to a DateTime value and then apply formatting
Do some string operations to extract the date part from your string value.
String operations:
string openDate = "2012-03-08 00:00:00";
string formatted = openDate.Substring(0, openDate.IndexOf(' '));
DateTime Parsing.
DateTime parsedDateTime = DateTime.Parse(openDate);
string formattedDateTime = parsedDateTime.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);

You need to convert your date into a DateTime object first. See examples here if your string is in a different or custom format.
openDate = !string.IsNullOrEmpty(node.ChildNodes[f].Attributes["ows_PMO_x0020_Origination_x0020_Date"].Value)? node.ChildNodes[f].Attributes["ows_PMO_x0020_Origination_x0020_Date"].Value: "" ;
//openDate is a string at this point. You'll need to convert it to a datetime object first, for the following line to work:
var dtObject = DateTime.Parse(openDate);
//Format the newly created datetime object
openDate = String.Format("{0:MM/dd/yyyy}", dtObject);

You can format datetime using:
If it is a datetime:
OpenDate = OpenDate.ToString("yyyy-mm-dd");
If the datatype is not datetime and you are sure the format will always be that then you can always convcert the string to datetime and use the method described above.
Convert.ToDateTime(openDate).ToString("yyyy-mm-dd");

The answers are great, especially if you would like to control the format of your 'time' part. Here is teh simplest way to get what you are after:
var dt = Convert.ToDateTime("2012-03-08 00:00:04");
Console.WriteLine(dt.ToLongTimeString());
Console.WriteLine(dt.TimeOfDay);
Output:

Use the following - openDate = openDate.Date

Related

Razor-pages ParseExact string

I am trying to format this string into a "MM-dd-yyyy" I am not sure what I am doing wrong I know its a string that I need to get ParseExact into a date.
20210921124857177 is the value and the error I am getting is not in an acceptable format
#{var contentLastModified = #item.GetValue("LastModified").ToString();
DateTime dateTimeLastModified = DateTime.ParseExact(contentLastModified, "MM-dd-yyyy",CultureInfo.InvariantCulture);
}
<div>LastModified : #dateTimeLastModified</div>
The value 20210921124857177 does not match the format MM-dd-yyyy.
At a guess, the format you need would be yyyyMMddHHmmssfff:
DateTime dateTimeLastModified = DateTime.ParseExact(contentLastModified, "yyyyMMddHHmmssfff", CultureInfo.InvariantCulture);
/* Result: 2021-09-21 12:48:57 */

Parse String date to specific format

This is a String date:
dob = reader.GetValue(7).ToString();` return like "12/2/2012"
But I want to convert (before passing it) to this format "2012212", I have tried
string newDate = dob.ToString("yyyMMdd")
But I got the following error:
The best overloaded method match for
'string.ToString(System.IFormatProvider)has some invalid arguments
any idea ?
Not Exact way of getting output but it will work.
string dob = "12/2/2012";
string[] d1 = dob.Split('/');
string s = d1[2] + d1[1] + d1[0];
You could use SqlDataReader.GetDateTime if underlying return type is DateTime
Then you just need..
reader.GetDateTime(7).ToString("yyyyMdd");
In case, if it is stored and received as a string then I would suggest converting to DateTime first and then look for specific format.
var date = DateTime.ParseExact(dob, "dd/M/yyyy",CultureInfo.InvariantCulture);
var formattedDate = date.ToString("yyyyMdd");
DateTime.ParseExact(reader.GetValue(7), "dd/M/dd", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyyMMdd");
If you have a date in a String with the format "ddMMyyyy" and want to convert it to "yyyyMMdd" you could do like this:
DateTime dt = DateTime.ParseExact(dob, "ddMMyyyy",
CultureInfo.InvariantCulture);
dt.ToString("yyyyMMdd");
You can try below method to convert string to date.
IFormatProvider culture = new CultureInfo("en-US", true);
dob = DateTime.ParseExact(reader.GetValue(7).ToString(), "dd/MM/yyyy", culture).ToString("yyyyMMdd");
Thank you.

getting issue with conversion from string to DateTime

I am getting issue while converting string to DateTime.
The value I am receiving as "08-26-2015 10:14:57.898Z".
I am trying to convert the above string to DateTime.
My Code:
DateTime.ParseExact(element.Value,"MM/dd/yyyy HH:mm:ss",CultureInfo.CurrentCulture);
Exception:
String was not recognized as a valid DateTime.
You have string with different format than you trying for conversion.
Try this
var input = "08-26-2015 10:14:57.898Z";
var date = DateTime.ParseExact(input, "MM-dd-yyyy hh:mm:ss.fff'Z'", CultureInfo.InvariantCulture);
You can use:
DateTime dt = DateTime.ParseExact("08-26-2015 10:14:57.898Z", "MM-dd-yyyy hh:mm:ss.fff'Z'", CultureInfo.InvariantCulture);
If you use CultureInfo.CurrentCulture(or null) the slash / has a special meaning. It is replaced with the current culture's date separator. Since that is not - but / in US you get an exception. Read
Have you tried Convert.ToDateTime ?
I just tried with your string and it works fine :
var s = "08-26-2015 10:14:57.898Z";
var date = Convert.ToDateTime(s);
String s = "08-26-2015 10:14:57.898Z";
DateTime date;
DateTime.TryParse (s, out date);
Now date variable contains DateTime value you need.

Why can't I parse this string to date?

I am trying to parse a string to a DateTime:
string datum = "13/7/2013";
DateTime dtDatum = DateTime.ParseExact(datum, "yyyy-d-M", CultureInfo.GetCultureInfo("nl-NL"));
I'm getting "FormatException was unhandled by user code". I've tried several formats, and also different CultureInfo's, but nothing seems to work. I've searched on Google and this website, but can't seem to find an answer that gets rid of the exception.
Help is much appreciated.
Your input format is not the same as the "format specifier that defines the required format of" the input.
The DateTime.ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the format parameter.
So your input would need to be string datum = "2013/13/7";; for this to match your format specifier.
Do not specify format, culture will do the job:
string datum = "13/7/2013";
DateTime dtDatum = DateTime.Parse(datum, CultureInfo.GetCultureInfo("nl-NL"));
This will parse "13/10/2013" also.
ParseExact needs the day parsed to be in the exact format specified:
So either:
string datum = "13/7/2013";
DateTime dtDatum = DateTime.ParseExact(datum, "d/M/yyyy", CultureInfo.GetCultureInfo("nl-NL"));
or
string datum = "2013-13-7";
DateTime dtDatum = DateTime.ParseExact(datum, "yyyy-d-M", CultureInfo.GetCultureInfo("nl-NL"));
string datum = "13/7/2013";
DateTime dtDatum = DateTime.ParseExact(datum, "d/M/yyyy", CultureInfo.InvariantCulture);

how to convert date with 'T' to/from string in C#

I used following functions to convert DateTime from/into string:
DATE_OBJ.ToString(DATE_FORMAT);
DateTime.ParseExact(Date_string, DATE_FORMAT, null);
Now I've got to work with follow format 2012-03-20T14:18:25.000+04:00
Which format should I use to convert it correctly to string and generate string like that from DateTime object?
You can go from DateTime to that format with
DateTime dt = new DateTime();
dt.ToString("o");
and from that format to DateTime with
DateTimeOffset.Parse(dateString);
Here is some more info on DateTime format:
http://www.dotnetperls.com/datetime-format
You are better of using DateTimeOffSet like:
string str = " 2012-03-20T14:18:25.000+04:00";
DateTimeOffset dto = DateTimeOffset.Parse(str);
//Get the date object from the string.
DateTime dtObject = dto.DateTime;
//Convert the DateTimeOffSet to string.
string newVal = dto.ToString("o");
You cannot do this from DateTime, as DateTime holds no TimeZone info.
This is close: string.Format("{0:s}", dt) will give 2012-03-20T14:18:25.
See: http://www.csharp-examples.net/string-format-datetime/
You could extend this to: string.Format("{0:s}.{0:fff}", dt), which will give 2012-03-20T14:18:25.000
But you better have a look at DateTimeOffset: DateTime vs DateTimeOffset
(Not advisable, but to fake it and still use DateTime: string.Format("{0:s}.{0:fff}+04:00", dt))
If that is a string you receive then you can split the string by T and use only the first part which is the Date component of the whole string and parse that.
ex:
string dateTimeAsString = "2012-03-20T14:18:25.000+04:00";
string dateComponent = dateTimeAsString.Splic('T')[0];
DateTime date = DateTime.ParseExact(dateComponent, "yyyy-MM-dd",null);

Categories