get exact date time from x date time range [closed] - c#

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

Related

How to get the date from the given string in c# [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 2 years ago.
Improve this question
I only need date from this string Wed, 02/05/2020 - 12:31 what format should I use whenever I am using
{MM/dd/yyyy} I am getting the same value if there is any other way please let me know
item.changed=Wed, 02/05/2020 - 12:31
`if (ListRecord.checkresponse == "Response Letters")
{
string checkresponse = item.changed;
string issueDate = string.Format("{0:MM/dd/yyyy}", checkresponse);
}`
To use date formatting, you need to start with a DateTime object, not a string.
So in your scenario you'll have to parse your string as a DateTime, then format it out again to a different string format. There's no way to directly format string -> string, all the logic about formatting is in the DateTime class.
Here's an example:
string changed = "Wed, 02/05/2020 - 12:31";
var checkresponse = DateTime.ParseExact(changed, "ddd, MM/dd/yyyy - HH:mm", CultureInfo.InvariantCulture);
string issueDate = string.Format("{0:MM/dd/yyyy}", checkresponse);
Console.WriteLine(issueDate);
Demo: https://dotnetfiddle.net/bhNImo
(Alternatively, since what you mainly want to do here is strip the first and last parts of the string and keep the date part, you could use a regular expression do to this from the string, but overall it's probably more reliably to use date parsing and formatting.)

I have a string like this "15.May-2019" and I want to replace May with 05 [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 2 years ago.
Improve this question
(so it looks like this "15.05-2019")but I don't want to remove the dot or the dash.How do I do this in C#
You could parse it to a DateTime and then format it back.
var result = DateTime.ParseExact("15.May-2019", "dd.MMMM-yyyy", System.Globalization.CultureInfo.InvariantCulture)
.ToString("dd.MM-yyyy");
This is useful if you need to handle dates with any month in them.
Note that the MMMM will work for long month names, but if you're dealing with short names MMM would be the way to go (for May the short and long names are the same). You can also use the overload of ParseExact that takes multiple formats if you need to handle both.
var str = "15.May-2019";
str = str.Replace("May", "05")
To replace part of a string you can do:
var date = "15.05-2019";
date = date.Replace("05", "May");
Console.WriteLine(date);
output : "15.May-2019"

how to change date at a specific time [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 5 years ago.
Improve this question
I m very new to C# and have some issue. I've a project where I need to change the date when School starts at 0700 am only not at 1200 am. I tried something like this but doesn't work
System.TimeSpan timeDiff = TimeSpan.FromHours(.292);
DateTime presentDate = (Convert.ToDateTime(sysDateTime)).Subtract(timeDiff);
I used this way in VB6 where I just substract .292 from current dateTime to change the date at 0700 am instead of 1200 am but dont know how do I achieve the same in C#.
You can try with
DateTime d = (Convert.ToDateTime(sysDateTime)).Subtract(new TimeSpan(5, 0, 0));

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

How can i convert system datetime format to Sql Datetime Format [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 7 years ago.
Improve this question
I am using Entity Framework Database First Approach and
I want to move Date first object to second object First Object is System.DateTime and the date Format is (01-10-2015) but i want to (2015-10-01) for Second Object After Converting Date in to second Object the Second Object inset in to data base
Can anyone Help me
Use DateTime.ParseExact:
DateTime temp = DateTime.ParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
You need to specify the format:
DateTime.ParseExact("10/01/2015 12:00:00 AM", "yyyy-MM-dd", CultureInfo.InvariantCulture)

Categories