JGuizard
3/28/2017 - 4:46 PM

Get date and time as string

Get date and time as string

//http://www.globalnerdy.com/2016/08/22/how-to-work-with-dates-and-times-in-swift-3-part-2-dateformatter/

static func getDatetimeAsString()->String
{
        let date = Date()
        let calendar = Calendar.current
        let year = calendar.component(.year, from: date)
        let month = calendar.component(.month, from: date)
        let day = calendar.component(.day, from: date)
        let hour = calendar.component(.hour, from: date)
        let minute = calendar.component(.minute, from: date)
        let second = calendar.component(.second, from: date)

        
        return String(day)+"/"+String(month)+"/"+String(year)+" "+String(hour)+":"+String(minute)+String(second)
}

    static func getDateAsString()->String
    {
        let date = Date()
        let calendar = Calendar.current
        let year = calendar.component(.year, from: date)
        let month = calendar.component(.month, from: date)
        let day = calendar.component(.day, from: date)

        return String(day)+"_"+String(month)+"_"+String(year)
    }
    
    
        static private func dateConverter(sdate:String,format:String)->Date
    {
       format=yyyy/MM/dd HH:mm:ss //24h format
      format=yyyy/MM/dd hh:mm:ss //12h format
      sdate = "2015/3/07 19:00:1"

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        print(sdate)
        return dateFormatter.date(from: sdate)!
    }