0

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.

gamered123
  • 57
  • 1
  • 9

4 Answers4

1

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)) 
Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
0

you can do with c#

DateTime date1;
DateTime date2;
return (date1 - date2).Days;
Dhaval
  • 2,341
  • 1
  • 13
  • 16
0

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
Brijesh
  • 91
  • 1
  • 14
0

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

Kishore Sahasranaman
  • 4,013
  • 3
  • 24
  • 50