NSDate/NSDateFormatter - Storing only time, not date?

前端 未结 4 1676
谎友^
谎友^ 2021-02-20 05:44

I\'ve been looking around but I haven\'t seen anything that addresses this so I\'m hoping someone can help clear this up for me. What I am trying to do is use an NSDate variable

4条回答
  •  爱一瞬间的悲伤
    2021-02-20 06:14

    Swift 4 If you want to use time with today's date

    let todate = Date()
            let formater = DateFormatter()
            formater.dateFormat = "yyyy:MM:dd"
           let date = formater.string(from: todate)
            print(date)
            let completeDate = date + ":9:00AM"
            print(completeDate)
            let anotherFormater = DateFormatter()
            anotherFormater.dateFormat = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ"
           anotherFormater.date(from: completeDate)
    

    if you want to add time in any date then:-

      let completeDate = "2019-02-15" + ":9:00AM"
                print(completeDate)
                let anotherFormater = DateFormatter()
                anotherFormater.dateFormat = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ"
               anotherFormater.date(from: completeDate)
    

提交回复
热议问题