how to convert date and time to date [duplicate] - c#

This question already has answers here:
format date in c#
(7 answers)
Closed 8 years ago.
i have this string "1/20/2015 12:00:00 AM" and i need to convert in something like this "20/1/2015" without time and days first.
DateTime mydate = Convert.ToDateTime(myDateFromDB);

To convert date string of format "dd/MM/yyyy" to DateTime, should used DateTime.TryParseExact where you can specify the date format string to let the converter know about the Culture.
To get the Date alone, use myDateTimeObj.Date.

Related

How to format string "1441/10/15" exactly same to DateTime C#? [duplicate]

This question already has answers here:
Format date in C#
(7 answers)
Closed 2 years ago.
I have string date in this format "1441/10/15" i want to change it to exact same format "1441/10/15" in datatime.
i tried to change it like this,
var yesy = Convert.ToDateTime(emp.datepicker);
but it is changing it but format is different that is "{15/10/41 12:00:00 ص}"
I need exact same format that is "1441/10/15"
thanks for your suggestion
EDITED:
I have string date "1441/10/15" how can i save it as it is in sql data base as datetime.
DateTime dt = DateTime.Parse(emp.datepicker);
string yesy = dt.ToString("yyyy/MM/dd");
Console.WriteLine(yesy);
Output = "1441/10/15"
I presume that "{15/10/41 12:00:00 ص}" is the output of yesy.ToString() in the code you didn't show to us.
Try yesy.ToString("yyyy'/'MM'/'dd").
For more information about possible output format, the doc is here as Bagus Tesa commented.

Convert string in to DateTime [duplicate]

This question already has answers here:
Converting a String to DateTime
(17 answers)
Parse a string containing date and time in a custom format
(3 answers)
Closed 4 years ago.
I have a string: "20180830" which represents 30 august 2018
I want to go to string: "30/08/2018"
So that I can do: DateTime parsedDate = DateTime.Parse("30/08/2018"); and have a DateTime instead of a string,
Tried everything but didn't succeed.
Needs some help.
You can use the DateTime.ParseExact-Method to solve your problem. Therefore you need to specify the exact format which would be yyyyMMdd in your case. Also the documentation suggests to use CultureInfo.InvariantCulture.
The following code...
DateTime datetime = DateTime.ParseExact("20180830", "yyyyMMdd", CultureInfo.InvariantCulture);
...should do the trick ;-)

Parsing a integer using DateTime.ParseExact [duplicate]

This question already has answers here:
How do I convert an Excel serial date number to a .NET DateTime?
(4 answers)
Closed 4 years ago.
I am trying to parse a integer(like 43392) to a date time.
The code looks like this:
DateTime d = DateTime.ParseExact(item[11], "dd/MM/yyyy", CultureInfo.InvariantCulture);
wher item[11] is "43392".
This throws an System.FormatException error, string was not recognized as a valid Datetime.
Need some guidance on this.
This should work
DateTime dateTime = DateTime.FromOADate(43392);
Output
19/10/2018 12:00:00 AM
DateTime.FromOADate Method (Double)
Returns a DateTime equivalent to the specified OLE Automation Date.

C#: Parse Datetime.Now to specific fromat [duplicate]

This question already has answers here:
Given a DateTime object, how do I get an ISO 8601 date in string format?
(18 answers)
Closed 6 years ago.
I want to parse Datetime.Now into formatted string like
"2016-09-01T02:00:12.7011585Z"
You can't parse DateTime.Now. It already returns a DateTime, not a string.
You can generate it's string string representation using yyyy-MM-dd'T'HH:mm:ss.fffffff'Z' format with a proper culture like InvariantCulture;
DateTime.Now.ToString("yyyy-MM-dd'T'HH:mm:ss.fffffff'Z'", CultureInfo.InvariantCulture)

DateTime.date give me a full Date why? [duplicate]

This question already has answers here:
how to give format DateTime.Date?
(7 answers)
Closed 7 years ago.
I need only the date from a DateTime class so I use DateTime.Date,
but it gives me a Full DateTime format, like this:
DateTime date = new DateTime();
date.Date
give me:
+ date {12/01/2016 00:00:00} System.DateTime
I only need the:
date {12/01/2016}
12/01/2016 00:00:00 is the same as 12/01/2016 as a value.
If you wanna get 12/01/2016 as a string representation, you can use .ToString method like;
var str = date.Date.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
If you wanna get this 12/01/2016 as a DateTime, simply you can't. A DateTime instance always has date and time part. That's why you see it as 12/01/2016 00:00:00 on debugger even if you set it's time part to midnight.

Categories

Resources