我从服务器而不是设备日期获取日期。但是更改日期存在问题
override func viewDidAppear(_ animated: Bool) {
datepicker2.datePickerMode = .date
datepicker2.timeZone = TimeZone(identifier: "IRST")
datepicker2.calendar = Calendar(identifier: .persian)
datepicker2.locale = Locale(identifier: "fa_IR")
datepicker2.date=self.minDate // this is a date var filled
datepicker2.addTarget(self, action: #selector(datepickerAction(_:)), for: .valueChanged)
}
每次更改日期时,打印的日期仅为一个日期
func datepickerAction(_ sender: UIDatePicker){
let date = self.datepicker2.date
let components = Calendar.current.dateComponents([.month, .day,.year], from: date)
let hour = components.month
let minute = components.day
let yr=components.year
print(hour,minute,yr) // prints one date always
}
这是我从服务器获得的日期:
func CurrentdateAndTime(){
let url=URL(string: "\(address)date/getNewDate")
var urlrequest=URLRequest(url: url!)
print(url)
var time:String=""
urlrequest.httpMethod = "GET"
URLSession.shared.dataTask(with: urlrequest) { (data, response, error) in
if let data=data {
let json=String(data: data, encoding: String.Encoding.utf8)
let b:Double=Double(json!)!
// if !b.isZero{
self.setComperehensiveDate(longDate: b)
self.settime(longTime: b)
self.setDay(day: b)
self.setYear(year: b)
self.setmonth(month: b)
self.setHour(longTime: b)
self.setMin(longTime: b)
self.onComplete?("ok?")
}
}.resume()
}
func setComperehensiveDate(longDate:Double){
let dbl = TimeInterval(longDate)
let date = Date(timeIntervalSince1970: dbl / 1000)
//let formatter = DateFormatter()
print("long",longDate,dbl)
// formatter.calendar = Calendar(identifier: .persian)
// formatter.locale = Locale(identifier: "fa_IR")
// formatter.timeZone = TimeZone(identifier: "IRST")
//formatter.dateFormat = "yyyy-MM-dd HH:MM:ss"
//let a = formatter.string(from: date)
// resultDate = formatter.date(from: a)
resultDate = date
print("aaa",resultDate)
}
我从func上设置的服务器获取毫秒数,然后将其转换为日期
我以这种方式获取minDate数据:
self.minDate = getcomperehensiveDate()
update:我的问题是用户是否手动将设备日期设为过去或将来的选择器显示了我不想要的过去。我必须显示当前日期,这就是为什么我要编程设置日期并面对此问题的原因,那么您对此情况有何看法?
答案 0 :(得分:0)
datepickerAction
的值后,调用 datepicker2
。
以下内容应正常工作:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
datepicker2.datePickerMode = .date
datepicker2.timeZone = TimeZone(identifier: "IRST")
datepicker2.calendar = Calendar(identifier: .persian)
datepicker2.locale = Locale(identifier: "fa_IR")
// The following line is setting the datepicker2.date to Tomorrow, because you haven't posted the code giving self.minDate
datepicker.date = Date().addingTimeInterval(24 * 3600)
}
func datepickerAction(_ sender: UIDatePicker) {
let date = self.datepicker2.date
let components = Calendar.current.dateComponents([.month, .day,.year], from: date)
let month = components.month //This is the month component, not the hour like in your code
let day = components.day //This is the day component, not the hour like in your code
let yr = components.year
print(day!,month!,yr!)
}
如果您想要小时和分钟的组成部分,则只需定义components
,如下所示:
let components = Calendar.current.dateComponents([.minute, .hour, .month, .day,.year], from: date)
let hour = components.hour
let minutes = components.minute
答案 1 :(得分:0)
我可以看到您的线路有问题
let date = self.datepicker2.date
必须是相同的日期或其他日期。我可以看到您的约会日期是
datepicker2.date=self.minDate (this is issue)
我刚刚尝试使用
@IBAction func change(_ sender: Any) {
let components = Calendar.current.dateComponents([.month, .day,.year], from: datepicker.date)
let hour = components.month
let minute = components.day
let yr=components.year
print(hour,minute,yr)
}
同一个选择器工作正常。 请从链接下载项目:
答案 2 :(得分:0)
您的问题是因为您在viewDidAppear中设置了UIDatePicker日期。 viewDidAppear和viewWillApear不够可靠,无法用于调用一次时间设置器,请使用viewDidLoad,因为这保证只能被调用一次。
我很难学到这一点,在有问题的日期设置器中设置了断点后,我将其追溯到在viewDidLayoutSubviews覆盖中设置的事实。显然,另一个禁忌使我想到了你的问题。