'string' to 'System.IFormatProvider' Error [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 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.

Related

How do I split a string in 2 with a colon in? [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 17 days ago.
This post was edited and submitted for review 17 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a textbox with an entry field, in which a user inputs time in a 00:00 format (HH:mm). What I would like to do is extract 2 integers from the string, the Hours (20:00 = 20 hours) and the minutes (00:20 = 20 minutes).
To clear things up, I tried using the string.Split() method with ":" as my delimiter. I was fetching my string from a TextMeshProUGUI Input Field in unity and when Split(); the resulting substring wouldn't convert into an Integer for some reason so I settled on using 2 separate (LEGACY) InputFields, one for hours one for minutes which works but is clunky.
I'm going to re-try my single InputField using the Legacy UI and the string.Split() method explained here.
Extending my comment into an answer. It seems like you're about to parse a string into a time which you can achieve with TimeSpan class and extract needed informations easily. For that you need to know what format your time input is and how you want to process it. Judging by your code I can assume you always expect a format of hh:mm which indicates that first part is an hour in 2 digit format ( either 02:mm or 14:mm ), same with minutes. This alone solves most of your problems because you can just parse your string into TimeSpan using ParseExact method:
// first, create a format you want to be used
const string FORMAT = "hh:mm";
// now just use TimeSpan.ParseExact method to retrieve your data
var result = TimeSpan.ParseExact(YourTextBoxText, FORMAT);
This will return a TimeSpan which has properties like Hours, Minutes etc. which you can then use to retrieve whatever time value you want.
EDIT: Things to note are that the above can throw a bunch of exceptions depending on the data input, formats etc. It's up to you if you're about to make any custom logic based on exception or whatever. Exceptions you might need to include in your logic and be aware of are
ArgumentNullException - thrown whenever argument is null,
FormatException - thrown whenever text input is in invalid format
For more info about TimeSpan you shold refer to the documentation
You could use the TimeSpan.Parse() function to convert the string to a TimeSpan.
Not the fastest method, but it's clean.
string time = "20:20";
if (TimeSpan.TryParse(time, out TimeSpan timespan))
{
// Do something with:
// timespan.Hours
// timespan.Minutes
}

Date is not displaying in correct format [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 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.

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;.

override default DateTime format in C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am working in an application built in .NET framework 2.0 but I am running it in .NET framework 4.0, with SQL server as a database.
when I try to take date as a input from Calendar controller (Webforms) its in the format dd/MM/yyyy and this application using Convert.ToDateTime() to parse this date and it throws exception.
so how can I resolve this problem and is there any way that DateTime class object store the date in specific format means I want to store date in dd/MM/yyyy format even when I copy this date object to another, then both object should have same format.
and sorry for My English :p
Assuming you mean this Calendar control, you should just use the SelectedDate or SelectedDates property - let the control handle the conversions.
In general, you should avoid performing textual conversions unless you really need to. (This includes when you interact with the database - use parameterized SQL, and simply pass the DateTime values in the parameters.)
var result = DateTime.ParseExact("29/01/2014", "dd/MM/yyyy", CultureInfo.InvariantCulture);
You should avoid converting DateTime to string and back if you can help it.

Categories