Date is not displaying in correct format [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am not that much experienced in asp.net MVC. I want to display date in correct format. But it is showing some number instead of that.
Below is my screenshot of the problem.
Here is my code where I convert date.
dateTime[i] = Convert.toDateTime(orderDetail.Rows[i]["joiningDate"].toString());

use this. you are not getting date in correct format so first convert it into string format and then to date time
Convert.ToDateTime(Convert.ToDateTime(orderDetail.Rows[i]["joiningDate"]).ToString("MM/dd/yyyy"))

To convert a date to a proper string, you can use DateTime.ToString(string format)
so:
DateTime.Now.ToString("dd-MM-yyyy")
will display as 10-03-2018.
So on your page you'll have to use .ToString on your date, with the format you want to use.

Related

'string' to 'System.IFormatProvider' Error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I am trying to get "ago" time within C# but am currently struggling as I get this Error.
Basically this is what I am trying.
var dateAgo = $"{DateTime.Now.Subtract(booking.CreatedAt.Date)}";
booking.createdAt.Date is where my date for my booking is saved.
I can show #dateAgo in my HTML but the problem is when I try something like
#DateTime.Now.Date.Subtract(booking.CreatedAt.Date).ToString("d")
which sadly does not work.
DateTime.Subtract() yields a TimeSpan object. ToString("d") is not a standard TimeSpan format string. See Standard TimeSpan Format Strings.
You want the custom format string "d" which outputs the number of days. For this, change "d" to "%d":
#DateTime.Now.Date.Subtract(booking.CreatedAt.Date).ToString("%d")
Or you can simply extract the Days from the returned TimeSpan object which will return the number of days:
#DateTime.Now.Date.Subtract(booking.CreatedAt.Date).Days.ToString()
Use DateTime.Now.Subtract(booking.CreatedAt.Date).Days
Subtract() produces a TimeSpan, which is incompatible with using the "d" format specifier by itself. Try something else, e.g. .ToString("%d") or .ToString("c").
Or if needed, the TimeSpan provides .Days, .Hours etc properties which can be used instead.

How to convert date format with T format in c# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a date format as shown below 2021-02-11T13:00:00+04:00 which i need to get in following format 11-FEB-20 01.00.00.000000000 PM.How can i achieve it ?
Parse and then format according to Custom date and time format strings
DateTime dt = DateTime.Parse("2021-02-11T13:00:00+04:00");
Console.WriteLine(dt.ToString("dd-MMM-yy"));
You can Parse form existing format into DateTime and then format it into String with the desired format string:
string source = "2021-02-11T13:00:00+04:00";
string result = DateTime
.Parse(source)
.ToString("dd-MMM-yy hh.mm.ss.fffffff'00' tt", CultureInfo.InvariantCulture)
.ToUpper();
Please, note
ToUpper() since we want FEB, not Feb.
fffffff'00' - we can't provide 9, but 7 digits after the decimal point, so we have to append two zeros.

why system.dattime cannot be converted to Timespan.? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a datetime field in sql table.
From c#, I also have datetime type but it is supposed to store only time so I am sending it 01/01/0001 09:49:44.
But while storing it in database it throws error:
that system.dattime cannot be converted to Timesapn.
Use the TimeOfDay property of the DateTime object
DateTime input = DateTime.Parse("01/01/0001 09:49:44");
TimeSpan result = input.TimeOfDay;

Why does DateTime.Parse() get wrong year when parsing dd-MMM date? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I created an application in which the user enter a month-year dd-MMM date as input, and this date is parsed using the DateTime.Parse() method. It is working fine on my PC.
Dim txtDate as String = combobox1.Text
Dim dtDate as DateTime = DateTime.Parse(txtDate)
Example:
DateTime.Parse("01-Dec")
Result:
2017-12-01 00:00:00
But when running this application on other machines, it returns 1899 year.
1899-12-01 00:00:00
After searching I found a similar question here: Time parsing Issue using DateTime.ParseExact() and it is marked as answered, but there is no solution for this situation
Does anyone knows what causes this problem?
Note: it is an old application I am working on. I know that it is recommended to replace it with a DateTime picker, but the issue is very confusing
Date.Parse without specifying an IFormatProvider will use the current user's Culture settings to determine what formats to parse, so 01/04/2017 will be 2017-April-01 on most computers (dd/MM/yyyy) but 2017-Jan-04 on computers in the US (MM/dd/yyyy).
If you're using the same format everywhere, you should use DateTime.ParseExact and provide an explicit format.
As for your specific problem, the string 01-Dec does not specify a year component, the computer must therefore infer the date, and how that date is inferred is often down to the Culture setting too.

How to get time only with date and time in C# [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have .CSV file and there is Date column but it has content date and time like follows;
24/02/2017 13:57:53
manageInstance.Date = DateTime.ParseExact(valuesInCsvLine[0], dateFormatString, CultureInfo.InvariantCulture);
So,how I get time only from this format.I am doing C# programming.If anyone know,please help me.I am newer to this language.
You can parse your String as DateTime like
var text = "24/02/2017 13:57:53";
var dateTime = DateTime.Parse(text);
And then you can access the time like
var time = dateTime.TimeOfDay;
You can get time like var timeOnly = DateTime.Now.TimeOfDay;.

Categories