使用sqlite3计算日期

时间:2010-03-12 16:41:32

标签: sql sqlite system.data.sqlite

我正在尝试在日期之间计算TimeSpans。如果使用原生sqlite3格式'YYYY-dd-mm'格式化日期,我对此没有任何问题

如果日期格式不同,我该怎么做呢,例如'dd-mm-YYYY'

我尝试了以下但没有成功。

- 选择两天之间的天数;如果日期时间字符串格式为YYYY-dd-mm

,则此方法有效
SELECT julianday(date1) - julianday(date2) AS Span from myTable;

- 我尝试使用dd-mm-YYYY格式的日期,但它似乎不起作用 - 似乎无法指定日期格式。

SELECT julianday(strftime('%d-%m-%Y', date1)) - julianday(strftime('%d-%m-%Y', date2)) AS Span from myTable;

1 个答案:

答案 0 :(得分:2)

由于您使用的是System.Data.SQLite,我建议使用自定义函数。这将更容易使用并与MS SQL Server保持一致,使其他.NET开发人员更清楚地理解和维护。

/// <summary>
/// MS SQL 2005 Compatible DateDiff() function.
/// </summary>
/// <remarks>
/// ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.SQL.v2005.en/tsqlref9/html/eba979f2-1a8d-4cce-9d75-b74f9b519b37.htm
/// 
/// 
/// </remarks>
[SQLiteFunction(Name = "DateDiff", Arguments = 3, FuncType = FunctionType.Scalar)]
public class DateDiff : SQLiteFunction
{
    public override object Invoke(object[] args)
    {
        if (args[0] == DBNull.Value || 
            args[1] == DBNull.Value ||
            args[2] == DBNull.Value)
        {
            return null;
        }
        string part = Convert.ToString(args[0]);
        DateTime startTime = ToDateTime(args[1]);
        DateTime endTime = ToDateTime(args[2]);

        switch(part)
        {
            case "year":
            case "yy":
            case "yyyy":
                return endTime.Year - startTime.Year;

            case "quarter":
            case "qq":
            case "q":
                return (endTime.Year - startTime.Year) * 4 + ((endTime.Month - 1) / 3) - ((startTime.Month - 1) / 3);

            case "month":
            case "mm":
            case "m":
                return (endTime.Year - startTime.Year) * 12 + endTime.Month - startTime.Month;

            case "dayofyear":
            case "dy":
            case "y":
            case "day":
            case "dd":
            case "d":
                return (endTime - startTime).TotalDays;

            case "week":
            case "wk":
            case "ww":
                return (endTime - startTime).TotalDays / 7.0;

            case "Hour":
            case "hh":
            case "h":
                return (endTime - startTime).TotalHours;

            case "minute":
            case "mi":
            case "n":
                return (endTime - startTime).TotalMinutes;

            case "second":
            case "ss":
            case "s":
                return (endTime - startTime).TotalSeconds;

            case "millisecond":
            case "ms":
                return (endTime - startTime).TotalMilliseconds;

            default:
                throw new ArgumentException(String.Format("Date part '{0}' is not recognized.", part));
        }

    }

    private static DateTime ToDateTime(object source)
    {
        try
        {
            return Convert.ToDateTime(source);              
        } 
        catch (Exception ex)
        {
            throw new ArgumentException(String.Format("DateDiff Input value '{0}' can not be converted to a DateTime.", source), ex);
        }
    }
}