I am using DaySquare calendar.
I have dateObjects array.
I have to show this dates in calendar but I am getting reason: 'Invalid month: 0' error.
import UIKit
import Daysquare
class ViewController: UIViewController {
var dateObjects = [Date]()
var calendarFromDateArr2 = [String]()
#IBOutlet var calendarView: DAYCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
calendarFromDateArr2 = ["2017-10-30T07:41:00", "2017-10-30T11:23:00", "2017-10-30T11:48:00", "2017-11-10T00:00:00", "2017-11-13T19:43:00", "2017-12-01T00:00:00", "2017-12-31T00:00:00"]
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateObjects = calendarFromDateArr2.flatMap { dateFormatter.date(from: $0) }
print(dateObjects)
for date in dateObjects
{
self.calendarView.selectedDate = date
}
}
}
I am getting single value when I give "return" after this line:
self.calendarView.selectedDate = date.
Related
I have a question about dateFormatter convert string to date.
because I try to convert date string, and my function always return nil when I using 19590401.
But When I using 19590402, it seems work fine.
What's wrong about my code?
I can't fix this bug?
Thanks.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let dateString: String = "19590401"
let format: DateFormatter = DateFormatter()
format.dateFormat = "yyyyMMdd"
format.locale = Locale(identifier: "zh_TW")
format.timeZone = TimeZone(identifier: "UTC+8")
if let date:Date = format.date(from: dateString) {
print("aaa : \(date)")
} else {
print("aaa : nil") //Always return nil.
}
}
}
In Taiwan daylight saving time changed on April, 1st 1959 at midnight so 00:00 didn't exist
https://www.timeanddate.com/time/change/taiwan?year=1959
When I use DatePicker to set date
It said it was nil
Despite I checked the date string format is correct
And dateFormat is set to format that I want.
Here's code
class DatePickerViewController: UIViewController {
var originDate: String!{
didSet{
//print("\(originDate)") is like "2019/07/12"
selectedDate = dateFormatter.date(from: originDate)
//print("\(selectedDate)") is nil
datePicker.setDate(selectedDate, animated: true)
}
}
var selectedDate: Date!
var dateString: String = "yyyy/MM/dd"
var dateFormatter: DateFormatter = DateFormatter()
override func viewDidLoad() {
dateFormatter.dateFormat = dateString
}
The ShowPickerViewController call DatePickerViewController
class ShowPickerViewController:UIViewcontroller {
#IBAction func showDatePicker(_ sender: Any) {
let vc = DatePickerViewController()
vc.originDate = sender.text // "2019/07/12"
present(vc, animated: true)
}
}
Initialize the date formatter with a closure
let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"
return formatter
}()
and delete the line in viewDidLoad
I have two datepickers that contains time with format 7:00 AM and 8:00 PM for login and logout. These times may vary. I wanted to change the logout time and tried to use the login time as the minimum time but my code does not work, I can still select time like 1:00 AM or 6:00 AM as logout.
This is the code in my ViewDidLoad
if let actualLoginText = loginTextField.text {
let start = actualLoginText
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
formattedTime = formatter.date(from: start)!
}
And this is the code of my logout picker
#objc func timeOutPickerValueChanged(sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
timeOutPicker.minimumDate = Calendar.current.date(byAdding: .minute, value: 5, to: formattedTime)
logoutTextField.text = formatter.string(from: sender.date)
}
I also tried this but still doesn't work
if let actualLoginText = loginTextField.text {
let start = actualLoginText
let formatter = DateFormatter()
formatter.dateFormat = "hh:mm a"
formattedTime = formatter.date(from: start)!
}
Thank you.
When working with dates / times, work with Date objects, not strings. The "string representation" should only be used to display the date/time to the user, not when manipulating the values.
Here is a simple example (assuming you have added two UIDatePickers and connected their Value Changed events). On load, it initializes the timeInPicker to the current date/time. Any time you select a new "Time In", the timeOutPicker.minimumDate is set to 5-minutes from the selected time, and the timeOutPicker.maximumDate is set to 8-hours from the minimum time:
class ViewController: UIViewController {
#IBOutlet weak var timeInPicker: UIDatePicker!
#IBOutlet weak var timeOutPicker: UIDatePicker!
let dateFormatter: DateFormatter = {
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .short
return df
}()
override func viewDidLoad() {
super.viewDidLoad()
// Initialize the TimeIn and TimeOut pickers to the current Date/Time
let now = Date()
updateTimeOutPicker(now)
}
func updateTimeOutPicker(_ withStartDateTime: Date) -> Void {
// add 5 minutes to the selected "In" time to get the "minimum time" for the Out Picker
guard let minDateTime = Calendar(identifier: .gregorian).date(byAdding: .minute, value: 5, to: withStartDateTime) else { return }
// add 8 hours to the to get the "maximum time" for the Out Picker
guard let maxDateTime = Calendar(identifier: .gregorian).date(byAdding: .hour, value: 8, to: minDateTime) else { return }
timeOutPicker.minimumDate = minDateTime
timeOutPicker.maximumDate = maxDateTime
print("New Min Out Time:", dateFormatter.string(from: minDateTime))
print("New Max Out Time:", dateFormatter.string(from: maxDateTime))
print()
}
#IBAction func timeInPickerValueChanged(_ sender: UIDatePicker) {
print("Selected In Time:", dateFormatter.string(from: sender.date))
print()
updateTimeOutPicker(sender.date)
}
#IBAction func timeOutPickerValueChanged(_ sender: UIDatePicker) {
print("Selected Out Time:", dateFormatter.string(from: sender.date))
print()
}
}
The login and logout time strings were pulled up from database. And when I don't edit the login textfield it's data remains as string since there was no value change, so I'm forced to use a string value as reference to my logout picker.
the result login being: 1999-12-31 23:00:00 +0000
and logout: 2018-01-04 22:24:20 +0000
this is the reason why using the login as my minimum time didn't work
so here's what I did, I added the current date so I can use it.
#objc func timeOutPickerValueChanged(sender: UIDatePicker) {
if let actualLoginText = loginTextField.text {
let date = Date()
let start = actualLoginText
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let nowTime = formatter.string(from: date)
let concat = nowTime + " " + start
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
formattedTime = dateFormatter.date(from: concat)!
}
let formatter = DateFormatter()
formatter.dateStyle = .none
formatter.timeStyle = .short
timeOutPicker.minimumDate = Calendar.current.date(byAdding: .minute, value: 5, to: formattedTime)
logoutTextField.text = formatter.string(from: sender.date)
}
Hi I am newbie to JTAppleCalendar. I need help.
I have installed it thru cocoapods and used some of the code as below.
I need help on how to use the basic functions such as:
Set the calendar for the current month
JTC.scrollToDate(Date())
show the current date on the calendar.
Do I need to specify or configure how many years this calendar support like 100 or 200 years?
Is this way correct to get a reference for the JTAppleCalendar
#IBOutlet weak var JTC: JTAppleCalendarView!
Please help to correct the code.
import UIKit
import JTAppleCalendar
class ViewController: UIViewController {
let formatter = DateFormatter()
#IBOutlet weak var JTC: JTAppleCalendarView!
override func viewDidLoad() {
super.viewDidLoad()
let d = Date()
var dateArr = [Date]()
dateArr.append(d)
JTC.scrollToDate(Date())
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: JTAppleCalendarViewDelegate,JTAppleCalendarViewDataSource{
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
formatter.dateFormat = "yyyy MM dd"
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
let startDate = formatter.date(from: "2017 01 01")!
let endDate = formatter.date(from: "2117 12 31")!
let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate)
return parameters
}
func calendar(_ calendar:JTAppleCalendarView,cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTAppleCell{
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.dateLabel.text = cellState.text
return cell
}
}
Thanks
The issue was solved for me by adding three methods in ViewDidLoad and setting start and end dates as follows:
override func viewDidLoad() {
self.calendarView.visibleDates {[unowned self] (visibleDates: DateSegmentInfo) in
self.setupViewsOfCalendar(from: visibleDates)
}
self.calendarView.scrollToDate(Date(),animateScroll: false)
self.calendarView.selectDates([ Date() ])
}
In Calendar configuration set dates as follows:
func configureCalendar(_ calendar: JTAppleCalendarView) -> ConfigurationParameters {
formatter.dateFormat = "yyy MM dd"
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
var dateComponent = DateComponents()
dateComponent.year = 1
let startDate = Date()
let endDate = Calendar.current.date(byAdding: dateComponent, to: startDate)
let parameters = ConfigurationParameters(startDate: startDate, endDate: endDate!, numberOfRows: 6, calendar: Calendar.current, generateInDates: .forFirstMonthOnly, generateOutDates: .off, firstDayOfWeek: .sunday, hasStrictBoundaries: true)
return parameters
}
I am teaching myself swift and I am still very new but I decided to make a simple app that prints the current time when you press a button. the code from the viewcontroller file is as follows:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBOutlet weak var LblTime: UILabel!
#IBAction func BtnCalltime(sender: AnyObject) {
var time = NSDate()
var formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM"
var formatteddate = formatter.stringFromDate(time)
LblTime.text = time
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
I am having an issue with the line:
LblTime.text = time
I keep getting the error:
Cannot assign a value of type 'NSDate' to a value of type 'String?'
I have tried using:
lblTime.text = time as! string?
And:
lblTime.text = time as! string
but it does still not work, I would be very appreciative of some help.
Thanks
You need use a value from formatter.
#IBAction func BtnCalltime(sender: AnyObject) {
var time = NSDate()
var formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM"
var formatteddate = formatter.stringFromDate(time)
LblTime.text = formatteddate
}
You made the string from an NSDate already, you just aren't using it.
lblTime.text = formatteddate
Date is now preferred over NSDate. It is an overlay class meaning both will work, but Date but has a lot of advantages, this answer lists some of those.
Here is how to format a date to a string using Date instead of NSDate.
var time = Date()
var formatter = DateFormatter()
formatter.dateFormat = "MMM d yyyy, h:mm:ss a"
let formattedDateInString = formatter.string(from: time)
dateLabel.text = formattedDateInString
A great site to get the formatter strings is http://nsdateformatter.com/
I had no idea that "MMM d yyyy, h:mm:ss a" would equal Mar 1, 7:02:35 AM but the site makes it easy.