SHARE
TWEET

Untitled

a guest Sep 16th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extension Date{
  2.   var month:String{
  3.     let locale = Locale.preferredLanguages[0]
  4.     let formatter = DateFormatter()
  5. //    formatter.locale = NSLocale.current
  6.     formatter.locale = Locale.init(identifier: locale)
  7.     formatter.setLocalizedDateFormatFromTemplate("MMMM")
  8.     return formatter.string(from: self)
  9.   }
  10.   var monthDays:Range<Int>{
  11.     return Calendar.current.range(of: .day, in: .month, for: self)!
  12.   }
  13.   var monthDay:Int{
  14.     return Calendar.current.component(.day, from: self)
  15.   }
  16.   var weekDay:Int{
  17.     return Calendar.current.component(.weekday, from: self)  - 1
  18.   }
  19.   var firstDayofMonth:Date {
  20.     var componets = Calendar.current.dateComponents( [.year, .month], from: self)
  21.     componets.day = monthDays.lowerBound
  22.     return Calendar.current.date(from: componets)!
  23.   }
  24.   var lastDayofMonth:Date {
  25.         var componets = Calendar.current.dateComponents( [.year, .month], from: self)
  26.         componets.day = monthDays.upperBound - 1
  27.         return Calendar.current.date(from: componets)!
  28.     }
  29.     var firstDayofWeek:Date{
  30.         var componets = Calendar.current.dateComponents( [.year,.month], from: self)
  31.         componets.day = monthDay - weekDay
  32.         return Calendar.current.date(from: componets)!
  33.     }
  34.   static
  35.     func makeTheDate(month:Int,day:Int,year:Int,hour:Int = 0 ,minute:Int = 0)->Date?{
  36.     let calendar = Calendar.current
  37.  
  38.     var components = DateComponents()
  39.  
  40.     components.day = day
  41.     components.month = month
  42.     components.year = year
  43.     components.hour = hour
  44.     components.minute = minute
  45.  
  46.     return calendar.date(from: components)
  47.   }
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top