DateTime? to string yyyy-mm-dd [duplicate] - c#

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 8 years ago.
I have a method that takes a parameter of the type DateTime?
I need to convert what I receive to a string of the format - yyyy-mm-dd
What is the cleanest optimal way to achieve this?

Assuming dateParameter if of type DateTime, just like you said:
dateParameter.ToString("yyyy-mm-dd");
EDIT:
Since you edited your post to nullable DateTime, here's an edit:
dateParameter.Value.ToString("yyyy-mm-dd");

You can use DateTime.ToString ..below is the sample code :-
datePassed.ToString("yyyy-mm-dd");
And for nullable DateTime :-
datePassed.Value.ToString("yyyy-mm-dd");

Related

C# - Parse Process.StartTime to a certain format [duplicate]

This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
DateTime ParseExact string was not recognize as a DateTime C#
(2 answers)
Closed 4 months ago.
I can't figure a way to parse a Datetime object that is "10/24/2022 4:01:35 PM" to "24/10/2022 16:01:35".
Can't find an answer for this exact format.
Thanks in advance!
I guess you don't need to parse that string to DateTime since you have it already in Process.StartTime and you're looking at it in the debugger. Then use it directly. If you really have it as string you simply need to use DateTime.Parse with InvariantCulture:
DateTime dt = DateTime.Parse("10/24/2022 4:01:35 PM", System.Globalization.CultureInfo.InvariantCulture);
and for the output:
Console.Write(dt.ToString("dd'/'MM'/'yyyy HH:mm:ss"));

C# convert roundtrip date string to DateTime object [duplicate]

This question already has answers here:
How to create a .NET DateTime from ISO 8601 format
(7 answers)
Closed 4 years ago.
I have to parse following string to DateTime object:
2018-03-27T14:33:54.4092769+03:00
I know that there are few methods that can parse string, for example DateTime.Parse() and DateTime.ParseExact(). I checked first method but there is no specification for this exact pattern.
So how can I parse this format to DateTime object?
That ISO 8601-format is already supported by DateTime.Parse/TryParse:
bool valid = DateTime.TryParse(strDate, out DateTime dt); // true

Convert String to DateTime Exactly Same format [duplicate]

This question already has answers here:
Parse string to DateTime in C#
(9 answers)
DateTime.Now.ToString("MM/DD/YYYY") contains incorrect month
(2 answers)
Closed 4 years ago.
How to convert string MM/dd/yyyy HH:ss to DateTime without changing the format. I just need to change the datatype from string to DateTime that's it. I have tried various ways but it doesn't give me my expected output.
String Input : 01/28/2018 20:00
Expected DateTime output : 01/28/2018 20:00 (only datatype should change)
Below are the some methods that I have tried:
DateTime.ParseExact("01/28/2018 20:00","MM/dd/yyyy HH:ss", CultureInfo.InvariantCulture): gives me 2018-01-28T20:00:00
Convert.DateTime("01/28/2018 20:00") gives me "String was not recognized as a valid DateTime" exception
DateTime.Parse("01/28/2018 20:00") this also gives me the same exception
Any help much appreciated.
As it was told in comments above, DateTime itself has no formatting, it happens only for string representation of the date and time. So, the best thing you can do here is to store two values: DateTime value itself and expected string format. Then, once converting your date & time into printable thing you just have to specify that stored format.

c# - how to convert 26/Jun/2016 at 13:14 to 2016-01-26 13:14:00 [duplicate]

This question already has answers here:
C# DateTime to "YYYYMMDDHHMMSS" format
(18 answers)
Closed 5 years ago.
i wanted to convert 26/Jun/2016 at 13:14 from a string to datetime data type
for saving to sql
string DtTime = ds.Tables["VOUCHER"].Rows[0]["BASICDATETIMEOFINVOICE"].ToString();
how to split as a string
You need to escape the / and the at in the format string, then you can use ParseExact:
DateTime.ParseExact("26/Jun/2016 at 13:14", "dd/MMM/yyyy 'at' HH:mm", CultureInfo.InvariantCulture);
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
But are you sure that the value is not stored as DateTime? Check by using ds.Tables["VOUCHER"].Rows[0].Field<DateTime>("BASICDATETIMEOFINVOICE"). Then no conversion from Object to string to DateTime is needed. If not you should consider to store it as DateTime in the first place, wherever the DataTable was filled from.

Convert a formatted date/time string into a DateTime object for comparison? [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Closed 5 years ago.
I'm writing an automation date and I want to extract a string, turn it into a DateTime object, and compare it with the current date and time.
The string in question has this format: 7/28/2017 1:17:29 PM
How can I convert it to a DateTime object to compare with the current time (Basically, my end goal is to verify that it is within a few minutes of the current time)
Use DateTime.Parse() or DateTime.TryParse()
System.Convert can convert to and from many types. For example...
int intElapsedMinutes = (DateTime.Now - Convert.ToDateTime("7/28/2017 1:17:29 PM")).TotalMinutes

Categories