How to get time only with date and time in C# [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 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;.

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.

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;

get exact date time from x date time range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Check the code bellow. In variable called- 'LastUpdatedAt' i am getting standard date time. Now my goal is i want to get the date time just 1 month back from this variable date. I already try to use DateTime.Now.AddMonths method but problem is i cant input my custom variable date on this method. Please tell me how can i get the date of 1 month back from that variable date? Screen shot attached for better understanding. see screen shot
var LastUpdatedAt = ctx.SearchedUsers.Where(x => x.SearchedUserID == item.SearchedUserID).FirstOrDefault().LastUpdatedAt;
DateTime.Now.AddMonths(1);
First you may you need to make DateTime type. Then you can use AddMonths() method on it. Try the code below this might work for you.
var SearchedUsers = ctx.SearchedUsers.Where(x => x.SearchedUserID == item.SearchedUserID).FirstOrDefault();
DateTime t = SearchedUsers.LastUpdatedAt.Value;
var outPut = t.AddMonths(-1);
var LastUpdatedAt = ctx.SearchedUsers.Where(x => x.SearchedUserID == item.SearchedUserID).FirstOrDefault().LastUpdatedAt;
DateTime myDate = DateTime.ParseExact(LastUpdatedAt, "dd-MMM-yy HH:mm:ss tt",System.Globalization.CultureInfo.InvariantCulture);
myDate .AddMonths(-1);

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.

I want get month from date that save in data base . How is it? [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 8 years ago.
Improve this question
How can I get the month from a date that is saved in data base.
int date = Convert.ToInt32(dr["ddate"].ToString());
Assuming that your date format is 'yyyy-MM-dd'
int month = DateTime.Parse(dr["ddate"].ToString()).Month;

Categories