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 6 years ago.
Improve this question
Yesterday. Here we subtract one day from the current day. We do this by adding -1 to the current day. This is necessary because no "Subtract Days" method is provided.
Note:
The example was run a few years ago. The output will naturally vary depending on the day you run it.
And:
DateTime.Today is always set to the machine's local time, which depends on the current system.
public DateTime GetLastDayOfMonth(int year, int month)
{
month += 1;
if (month>12)
{
month = 1;
}
return new DateTime(year, month, 1).AddDays(-1);
}
Related
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));
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 8 years ago.
Improve this question
I have a datatable column type TimeSpan. how would you subtract the Min of this timespan column from current time ? Appreciated.
Column name "Hours"
Calculate the minimum value :
var min = table.AsEnumerable()
.Min(r => r.Field<TimeSpan>("Hours"));
Subtract it from current time:
var newTime = DateTime.Now - min;
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 8 years ago.
Improve this question
Sorry, for my question. Actually i am not able to clarify my question for my hurry up.
I have a table like below :
This table is LoginHistoryTable(When a user login my application, his history insert this table according to UserId). I get all data from table as a list.
Now i want to Know : How many times they logged in this month and the last month.
This is really simple but here you go. You need to compare month and year.
boo isCurrentMonth = DateTime.Now.Month == otherDate.Month
&& DateTime.Now.Year == otherDate.Year;
Something like..
public bool IsThisMonth(DateTime dt)
{
return dt.Month == DateTime.Now.Month;
}
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 9 years ago.
Improve this question
How do I convert this the time the computer has been to sleep for (here called 'offTime') into an integer (in terms of minutes)?
`DateTime timeOnSleep = DateTime.Now;`
`Application.SetSuspendState(PowerState.Suspend, true, true);`
`DateTime timeOnWake = DateTime.Now;`
`System.TimeSpan offTime = timeOnWake - timeOnSleep;`
TimeSpan time = timeOnWake - timeOnSleep;
var hours = time.TotalHours;
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 8 years ago.
Improve this question
I want to run my Windows service at 9:00AM to 9:00PM and after the 9:00PM my all rows need to be saved to another table only one time and last day of month all records will be deleted and last day records will be saved..... please help me my Window service is running at 9:00AM to 9:00PM......
You can use a timer that fires every minute. And between 9:00 AM to 9:00 PM you set a flag to tell where to save the records.
The code that is executed could set:
if(DateTime.Now.Hour < 9 && DateTime.Now.Hour > 21)
TableToSaveTo = "secondTableToSaveTo";
else
TableToSaveTo = "primaryTableToSaveTo"
Then you use the value in TableToSave to when you do the actual saves