Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Tomorrow Date from custom date swift

Tags:

swift

i have an app and user can select date from that. How to Set Tomorrow Date from selected date. For example if user select date 24 it print 25 and if user select date 31 it print 1.

Right now i'm using current date to set tomorrow's date

let dates = Date()
var tomorrow: Date {
    return Calendar.current.date(byAdding: .day, value: 1, to: Date())!
}         

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd"

let dateOutDefault = dateFormatter.string(from: tomorrow as Date)

How to do something like that using custom date?

like image 256
Ariel Gemilang Avatar asked Mar 22 '17 07:03

Ariel Gemilang


1 Answers

import Foundation

extension Date {

    var tomorrow: Date? {
        return Calendar.current.date(byAdding: .day, value: 1, to: self)
    }
}

let today = Date()

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy, HH:mm b"

if let tomorrow = today.tomorrow {
    let tomorrowString = dateFormatter.string(from: tomorrow)
    print("\(tomorrowString)" // "Mar 23, 2017, 00:14 AM\n"
}
like image 105
pxpgraphics Avatar answered Nov 09 '22 05:11

pxpgraphics