日期格式,添加日期

时间:2018-09-10 09:36:33

标签: ios swift dateformatter

这是我的第一个问题;

SELECT
        t1.pId,
        t1.*,
        t2.username,
        t2.email,
        t2.customer_id 
    FROM
        b_actions t1 
    JOIN
        users t2 
            ON t1.user_id = t2.id 
    WHERE
        t1.action_name NOT IN (
            'action1', 'action2', 'action3'
        ) 
        AND t1.user_dismissed = 0 
        AND EXISTS (
            SELECT
                1 
            FROM
                temp_a 
            WHERE
                t1.pId = temp_a.id
        ) 
    ORDER BY
        t1.datetime DESC

如果我写print(“ date1”)或print(“ date2”),它给出下面的输出

  

2018-09-10 08:56:49 +0000

我想编写相同的示例,但是import Foundation let date1 = Date() let date2 = Date().addingTimeInterval(3600) if date1 == date2 { print("equals") } else if date1 > date2 { print("date1 is bigger") } else if date1 < date2 { print("date2 is bigger") } date1必须包含以下两个属性:

格式:date2

语言环境:"dd.MM.yyyy"

除此之外,这是我的第二个问题:

"tr_TR"

您知道,这3600个值增加了一个小时。如何增加一天? 24 * 3600?有没有最短的方法?

3 个答案:

答案 0 :(得分:1)

尝试

extension Date {
  func addDays(_ days: Int) -> Date {
    Calendar.autoupdatingCurrent.date(byAdding: .day, value: days, to: self)!
  }
}

答案 1 :(得分:0)

尝试这个

CLOB

答案 2 :(得分:0)

就像@Larme说的那样,您可能想研究Calendar

var dateComponents = DateComponents()
dateComponents.day = 1
guard let date = Calendar.current.date(byAdding: dateComponents, to: Date()) else {  // Adding date components to current day.
   fatalError("date not found")
}
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short // dd.MM.yyyy
dateFormatter.locale = Locale(identifier: "tr_TR") // Your preferred locale
let dateWithLocale = dateFormatter.string(from: date)
print(date)

您可以使用Date对象进行比较。仅在需要打印或将其用作String时,才需要进行格式化。