显示剩余天数

时间:2015-10-07 05:14:00

标签: c# asp.net

我有一个asp.net系统,它在数据库中有一个带有Datetime值的PODate字段。如何减去交货到达前的剩余天数。我想在列表视图的列中显示它

示例:交货日期是10月21日。它必须在交货到货之前显示14天。

可以在选择查询中完成,也可以在我的visual studio中完成。

4 个答案:

答案 0 :(得分:1)

如果您使用SQL Server,则可以使用此查询:

SELECT DATEDIFF(day,Column1,Column2) AS DiffDate from TableName

或者,如果您想在c#中执行此操作,则可以使用TimeSpan对象

DateTime date1;
DateTime date2;
return (date1 - date2).TotalDays;

如果您的子查询只返回一个值,那么您可以尝试

SELECT ABS((SELECT DATEDIFF(day,Column1,Column2) AS DiffDate from TableName)) 

答案 1 :(得分:0)

您可以使用 c#

DateTime date1;
DateTime date2;
return (date1 - date2).Days;

答案 2 :(得分:0)

这可以帮助您找到差异。

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

答案 3 :(得分:0)

可以在两者中完成,

  

我希望它能在Visual Studio中完成,因为这显然是在演示   逻辑。

<强> 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