I'm connecting to an API and when I get data back for dates it looks like this:
2016-07-05T21:39:17.696Z
How can I set a uitextfield.text to that date so it looks like a normal date (ie. 07/05/2016 or something similar)?
The tricky part is the NSDate is returned as a string from the API so it's not in the NSDate format to begin with.
A text field is only for editing freeform text, not dates. You can go one of two routes. If you intend for the user to be able to edit a date, then use a UIDatePicker to provide an interface specifically for editing dates. If you just want freeform text back, then you can convert a date to a string with NSDateFormatter.
try:
let formatter = NSDateFormatter()
let yourDate = NSDate()
formatter.dateStyle = NSDateFormatterShortStyle
formatter.timeStyle = NSDateFormatterNoStyle
self.textfield.text = formatter.stringFromDate(yourDate)
Related
I used FSCalendar(https://github.com/WenchaoD/FSCalendar) in my project. If user click repeat button, the events repeats everyday. I want to show it on calendar which is in my application. How can I do it?
If you want to select date then below method will be helpful,
where calendar is outlet of FSCalendar.
calendar.select(calendar.today)
And if you want to select multiple dates then the best way would be to use event dots, and that can be possible through below delegate method of FSCalendar Data source,
func calendar(_ calendar: FSCalendar, numberOfEventsFor date: Date) -> Int
{
//number of dots you want to show
}
or if you really want to show dates selected, you can use below method smartly and returning different colours for dates that you want to select,
public func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, fillDefaultColorFor date: Date) -> UIColor?
{
if your date
{
//return the colour for your dates
}
else
{
//return default colour
}
}
Hope, this answer helps you.
For Remainder app you need to do or use following thing.
1.Realm or Core Data - for Data persistency.
2.local notification - to notify user that some event is occured.
you need to use UILocalNotification that have one property fireDate property.
I don't know how you're persisting that information (the scheduled date), what i can tell you is that you can easily add a dot (or more if you'd like, just change the number) with the following delegate:
`func calendar(calendar: FSCalendar, numberOfEventsForDate date: NSDate) -> Int {
let formatter = NSDateFormatter()
formatter.dateFormat = "MM-DD"
let date1 = formatter.stringFromDate(yourDate)
let date2 = formatter.stringFromDate(date)
return date1 == date2 ? 1 : 0
}`
This method is one of FSCalendar delegates and it iterates all days in the current month (each one of them represented in date variable). In the example above i'm comparing if the any given date is equal to yourDate variable which has to be a NSDate as well by transforming both of them into Strings via NSDateFormatter in Month-Day format so the comparison is equal for all months. If true, return 1 (this indicates 1 dot, you may change it for more if you want to), otherwise return 0 dots below the specific date.
This code is Swift 2.3 compliance.
I'm writing an app with swift like list app. When i create a new task everything is working fine but a want to write a creation time on Subtitle text of table view's cell. How can i do that?
You can use NSDate to get the current date and time.
So in your creation method, (or object init):
let date = NSDate()
let dateFormatter = NSDateFormatter().Shortstyle
let dateString = dateFormatter.stringFromDate(date)
So I want to store dates in my CoreData entity and the model indicates the field should be of NSTimeInterval. Not sure why it's not NSDate though as NSTimeInterval sounds... odd.
From my JSON data source I have the following string: "2016-01-28T23:20:00" and I have no idea how to convert this to a NSTimeInterval. I can convert it to NSDate once I figure out the correct format, but that doesnt do much me much good.
Thanks!
You can store NSDate in CoreData Directly.
Update:
If you are using scalar propertie, convert like this:
// `NSTimeInterval` --> `NSDate`:
var date = NSDate(timeIntervalSinceReferenceDate: 123)
// `NSDate` --> `NSTimeInterval`:
var timeInterval = date.timeIntervalSinceReferenceDate
I implemented a UIDatePicker into my app. If I change the default date after the date picker was popping up, the date picker gives a valid date back. But if I'd like to choose the current date (I don't have to change the date picker) the date picker doesn't give a date back. Does someone knows a solution for this in Swift?
//Date Picker config.
func datePickerChanged(datePicker:UIDatePicker) {
var dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
var strDate = dateFormatter.stringFromDate(datePicker.date)
dateLabel.text = strDate
}
You have a callback when you change the date (valueChanged). So it is normal you don't get it when you just dismiss the picker without changing the date.
You have to know the date before you present your date picker, in case you don't set it, or if you want to cancel your selection if you handle it.
Create a var (I generally call it now ) where you store the currentDate (NSDate()) and set it to the date property of your UIDatePicker, so you know the date when the picker was presented since the default datePicker's date displayed is now.
If you set UIDatePicker's date in code, it doesn't run valueChanged events. You have to extract the date string update into a separate method and update it directly.
Also, don't treat the UIDatePicker as the sole date value source. Keep the model separate.
i.e.
var date = NSDate()
func datePickerChanged(datePicker:UIDatePicker) {
self.date = datePicker.date
self.updateDateStringWithDate(self.date)
}
func resetDate() {
self.date = NSDate()
self.datePicker.date = self.date
self.updateDateStringWithDate(self.date)
}
func updateDateStringWithDate(date: NSDate) {
var dateFormatter = UIDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
var strDate = dateFormatter.stringFromDate(date)
dateLabel.text = strDate
}
Also as a side note, don't create a new UIDateFormatter every time it's needed, because its initialisation is relatively expensive.
I've been following an Objective-C tutorial and the tutor is able to cast an NSTimeInterval object to a NSDate object.
The lesson uses CoreData, and stores the date of a post as an NSTimeInterval, later on we want to retrieve that interval and set it as a formatted date string to present as a section title in a UITableVIewController.
class DiaryEntry: NSManagedObject {
#NSManaged var date: NSTimeInterval
#NSManaged var body: String
#NSManaged var imageData: NSData
#NSManaged var mood: Int16
#NSManaged var location: String
func sectionName() -> String {
let date = NSDate().dateByAddingTimeInterval(self.date)
let f = NSDateFormatter()
f.dateFormat = "MMM yyy"
return f.stringFromDate(date)
}
}
Mainly concerned with the line:
let date:NSDate = NSDate().dateByAddingTimeInterval(self.date)
Which right now is actually adding the set date onto the current date, and this is not the behaviour I want.
How do I cast the self.date variable to an NSDate object in SWIFT?
There is a initializer of NSDate that takes NSTimeInterval:
init(timeIntervalSinceReferenceDate ti: NSTimeInterval)
So just do:
var date = NSDate(timeIntervalSinceReferenceDate: 123)
This code is really confused.
A time interval is not a date. A date is a point in time. A time interval is the difference in seconds between two points in time. Whenever you have a time interval, you have the question "it is the number of seconds between what two dates? You correctly realise that adding a time interval stored in a database to NSDate () is unlikely to give a useful result, because the same call executed 10 seconds later will give a different date.
The date of a post should most likely be stored as an NSDate object. Core Data handles NSDate objects just fine. If you want to store time intervals, the date of the post must be converted to a time interval since some fixed reference date; you do that for example using "timeIntervalSinceReferenceDate". If you do that, I very strongly recommend that you don't call the variable "date" but something like "secondsSinceReferenceDate" which makes it obvious what to store when you are given a date, and how to convert that number back to an NSDate.
(The reason to call it "secondsSinceReferenceDate" is because there is plenty of code that tries to store milliseconds, or nanoseconds, and there is plenty of code that stores intervals since the epoch (Jan 1st 1970), so it's really good if someone reading your code knows immediately what the numbers mean by just looking at the variable name).