Skip to content

Commit

Permalink
add convert method and next and before day
Browse files Browse the repository at this point in the history
  • Loading branch information
Shelock authored and Shelock committed Aug 5, 2018
1 parent fd42938 commit d549d58
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class ViewController: UIViewController {
super.viewDidLoad()
persianDatePickerView.onChange = {(year, month, day) in
print("\(year)/\(month)/\(day)")
print(self.persianDatePickerView.getGregorianDate()!)
print(self.persianDatePickerView.getPersianDate()!)
print("next day : \(self.persianDatePickerView.nextDay(by: self.persianDatePickerView.getPersianDate()!))")
}

}
@IBAction func didTapReset(_ sender: Any) {
persianDatePickerView.gotoCurrentDate()
Expand Down
2 changes: 1 addition & 1 deletion PersianDatePicker.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'PersianDatePicker'
s.version = '0.5.0'
s.version = '0.6.0'
s.summary = 'PersianDatePicker is a PickerView. by : farhad faramarzi'


Expand Down
Binary file not shown.
39 changes: 35 additions & 4 deletions PersianDatePicker/PersianDateDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Copyright © 2017 Farhad. All rights reserved.
//

import Foundation
import UIKit

struct PersianDateDataSource {
//MARK: - Properties
Expand All @@ -27,8 +27,10 @@ struct PersianDateDataSource {


func daysRange(ofYear year:Int, month: Int) -> [Int] {
let targetDayComponents = DateComponents(calendar: cal, year: year,
month: month, day: 1)
let targetDayComponents = DateComponents(calendar: cal,
year: year,
month: month,
day: 1)
guard let targetDay = cal.date(from: targetDayComponents) else { return [] }
guard let range = cal.range(of: .day, in: .month, for: targetDay) else { return [] }

Expand All @@ -39,5 +41,34 @@ struct PersianDateDataSource {
let date = Date()
let comps = cal.dateComponents([.year, .month, .day], from: date)
return (year: comps.year!, month: comps.month!, day: comps.day!)
}
}

func getPersianDate(year: Int, month: Int, day: Int) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")

let persianDate = "\(year)/\(month)/\(day)"
let date = dateFormatter.date(from: persianDate)
return date
}

func convertToGregorian(year: Int, month: Int, day: Int) -> Date? {
guard let persianDate = getPersianDate(year: year, month: month, day: day) else {return nil}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"

let gregorianDateString = dateFormatter.string(from: persianDate)
dateFormatter.calendar = Calendar(identifier: .persian)
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
return dateFormatter.date(from: gregorianDateString)
}

func nextDay(by date: Date) -> Date {
return cal.date(byAdding: .day, value: 1, to: date)!
}

func backDay(by date: Date) -> Date {
return cal.date(byAdding: .day, value: -1, to: date)!
}
}
16 changes: 16 additions & 0 deletions PersianDatePicker/PersianDatePickerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ public class PersianDatePickerView: UIView {
setDatePicker(year: year, month: month, day: day)
}

public func getGregorianDate() -> Date? {
return persianDateDataSource.convertToGregorian(year: year, month: month, day: day)
}

public func getPersianDate() -> Date? {
return persianDateDataSource.getPersianDate(year: year, month: month, day: day)
}

public func nextDay(by date: Date) -> Date {
return persianDateDataSource.nextDay(by: date)
}

public func backDay(by date: Date) -> Date {
return persianDateDataSource.backDay(by:date)
}

private func setDatePicker(year: Int, month: Int, day: Int) {
let yearIndex = persianDateDataSource.years.index(of: year)!
let monthIndex = month - 1
Expand Down

0 comments on commit d549d58

Please sign in to comment.