Reputation: 57
I have an asp.net system that have a PODate field with Datetime value in the database. How do I minus the days left until the delivery arrive. I want to show it in a column in a list view
Example: The date of delivery is on Oct 21. It must show 14 days to go before the delivery arrives.
Can it be done in a select query or it must be done in my visual studio.
Upvotes: 0
Views: 1090
Reputation: 2564
If you are using SQL Server
then you can use this query:
SELECT DATEDIFF(day,Column1,Column2) AS DiffDate from TableName
Or if you want to do this in c#
then you can use TimeSpan
object
DateTime date1;
DateTime date2;
return (date1 - date2).TotalDays;
If your Subquery is returning only one value then you can try
SELECT ABS((SELECT DATEDIFF(day,Column1,Column2) AS DiffDate from TableName))
Upvotes: 1
Reputation: 4230
It can be done in both,
I would prefer it to be done in Visual Studio, as this is clearly presentation logic.
C#
DateTime oldDate = new DateTime(2002,7,15);
DateTime newDate = DateTime.Now;
// Difference in days, hours, and minutes.
TimeSpan ts = newDate - oldDate;
// Difference in days.
int differenceInDays = ts.Days;
Console.WriteLine("Difference in days: {0} ", differenceInDays);
https://msdn.microsoft.com/en-us/library/aa287590%28v=vs.71%29.aspx?f=255&MSPPError=-2147217396
VB
Dim firstDate, msg As String
Dim secondDate As Date
firstDate = InputBox("Enter a date")
Try
secondDate = CDate(firstDate)
msg = "Days from today: " & DateDiff(DateInterval.Day, Now, secondDate)
MsgBox(msg)
Catch
MsgBox("Not a valid date value.")
End Try
https://msdn.microsoft.com/en-us/library/b5xbyt6f(v=vs.90).aspx
Upvotes: 0
Reputation: 91
may this help you to find differences.
SELECT DATEDIFF(DAY, GETDATE(), deliveryDate) AS DayDiff
SELECT DATEDIFF(MINUTE, GETDATE(), deliveryDate) AS MinuteDiff
SELECT DATEDIFF(SECOND, GETDATE(), deliveryDate) AS SecondDiff
SELECT DATEDIFF(WEEK, GETDATE(), deliveryDate) AS WeekDiff
SELECT DATEDIFF(HOUR, GETDATE(), deliveryDate) AS HourDiff
Upvotes: 0
Reputation: 2379
you can do with c#
DateTime date1;
DateTime date2;
return (date1 - date2).Days;
Upvotes: 0