Calculate a Future Date in Memory in WinForms - winforms

I have a Date in DateTimePicker I want to add say x no of days and then compare it with todays date in an if statement
Please Help me in Calculating the Date i.e how to add no of days to a Date
Please Help,Thanks in Advance

Use the AddDays method
Assuming you know how to get the DateTime from the datePicker,
DateTime dateFromPicker = DateTime.Now;
DateTime tomorrow = dateFromPicker.AddDays(1);
Console.WriteLine(tomorrow.ToString());

Related

Date conversion adding days

I'm doing the date conversion the following way:
CONVERT(VARCHAR, CONVERT(DATE, ZA2_DTFIM), 103) AS DATA
How can I add 5 days to that system date?
I tried several methods but so far nothing, I'm a beginner in SQL Server.
In TSQL the function GETDATE() provides the system date (and time).
To add 5 days to that use DATEADD(day,5,GETDATE())
To display this as a string you can use FORMAT() or CONVERT() e.g. both of these will add 5 days to the system date and then display that in day/month/year style:
select
convert(varchar,dateadd(day,5,getdate()),103)
, format(dateadd(day,5,getdate()),'dd/MM/yyyy')
Note: you do not need to convert to date because converting (or formatting) GETDATE() to dd/MM/yyyy will suppress display of time anyway.

Compare date to current month year

In one of the API I am using I have a date in the format yyyy-MM-dd hh:mm:ss
I am trying compare this date to just the current month and year. How do I just take out the month and year from that date and compare it in AngularJS?
I read the AngularJS Documentation for date formats and I can't seem just get the month and year from that format so that I can compare it with the current month and year
Refer momentjs docs.
Docs to parse date
Docs to get from date
Refer below code snippet,
var apidate = moment('2019-01-01 11:31:23', 'YYYY-MM-DD hh:mm:ss'); // API date
console.log("Day="+apidate.date()+ " Year="+apidate.year());
var now = moment(); // current date
console.log("Day="+now.date()+ " Year="+now.year());
// compare day and year of both dates
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Using momentjs.com
Convert the response from your API with eg;
responseDate = moment(responseAPI).format('YYYYMM')
You can then you can compare it with an existing date eg;
if (responseDate != moment(existingDate).format('YYYYMM')) { }

sql datetime converted to specific day if between certain times

I have a string of datetime records. I'd like to make a case condition or if statement which reads the datetime value and assigns this to a specific day.
The day range is 2:45PM up to the next day at 2:45PM. This range would be considered the 'next day'
Ex. Time of post is 3:42 PM 11/12/2016, therefore this would read as 11/13/2016. If this post was made at 2:42 PM 11/12/2016, then this would have read 11/12/2016.
So far don't believe CONVERT(date, 'datetime') will work due to time range constraints.
Thanks
Use DATEADD function to add time to the date so it gets to the next day. For 2:45PM you'll need to add 9:15.
Think I got it...check if datetime time value is above 2:45, if so, then convert to date and add a day, else convert to date.
CASE
WHEN CAST (MAIN.TimeOfRftDecision AS Time) > '14:45:00' THEN DATEADD(dd,1,CONVERT (Date, MAIN.TimeOfRftDecision))
ELSE CONVERT (Date, MAIN.TimeOfRftDecision)
END AS 'DayOfRft'

time portion from datetime value in LINQ

I have field which is of type DateTime.
When inserting a record, I am also providing the time into the field.
When reading that field with LINQ, it will return me the correct date but the time values has the default, 12:00:00 AM
In the database, I have values with date and time for each record which is correct. The problem is that the time changes to default when reading/databinding with LINQ.
dataGridView1.DataSource = IQueriableObject.Select(q => new
{AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay}) ;
What solution is there for extracting the time portion?
This is what is hurting you
AppointmentTime = q.AppointmentTime.Value.Date.TimeOfDay
When you do q.AppointmentTime.Value.Date it gets just the Date portion of the Value, by Default the TimeOfDay Property on a Date is 12:00:00 AM.
Instead you should do q.AppointmentTime.Value.TimeOfDay (this will only work if value is a type DateTime).

T-SQL: How to update just date part of datetime field?

In SQL Server 2008 I need to update just the date part of a datetime field.
In my stored procedure I receive the new date in datetime format. From this parameter I have to extract the date (not interested in time) and update the existing values date part.
How can I do this?
One way would be to add the difference in days between the dates to the old date
UPDATE TABLE
SET <datetime> = dateadd(dd,datediff(dd,<datetime>,#newDate),<datetime>)
WHERE ...

Resources