A parse object in my database has a date field. I want to use a date that the user selects as the query.
query.whereKey("dateFieldParse", equalTo: janFirst)
I'm not sure how to set up this "janFirst" field. I know it has to be somehow formatted for parse retrieval but what is this format.
I tried setting the components in an NSDate object but it doesn't seem to work.
let userCalendar = NSCalendar.currentCalendar()
let janFirstComponent = NSDateComponents()
janFirstComponent.year = 2014
janFirstComponent.month = 1
janFirstComponent.day = 1
janFirstComponent.hour = 0
janFirstComponent.minute = 0
var janFirst: NSDate = userCalendar.dateFromComponents(janFirstComponent)!
....
query.whereKey("dateFieldParse", equalTo: janFirst)
One more small thing: is the hours and seconds, and even the year, necessary for retrieval? I'm only really using the month and date.
The code for Parse and Swift still seems to be very limited and I can't seem to find anything at all for retrieving dates (+ I'm still very much a iOS/swift noob). Any help, even some general steps as to what to do would be greatly appreciated.
Thanks in advance. :)
I actually had it right, I just didn't realize timezones were an issue with Parse. It seems the timezone had to be set to "GMT" (Parse's timezone). For anyone that might face a similar issue, the wee bit of code is below.
let userCalendar = NSCalendar.currentCalendar()
let timeZone = NSTimeZone(abbreviation: "GMT")
userCalendar.timeZone = timeZone!
In your Parse data browser, make sure your parse object is set to Date and you variable is set to NSDate
Related
I trying to create a date object just containing the time of 1 second past midnight.
I believe the following should work but it just keeps returning nil.
let dateTime = Date()
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:mm:ss"
let time = timeFormatter.date(from: "00:00:01")
print("Time: \(time!)")
Can someone tell me what i'm doing wrong!
Thanks
Let Calendar do the math, this is more reliable, you aren't using the current date (dateTime) anyway.
let midnight = Calendar.current.startOfDay(for: Date())
let oneSecondAfterMidnight = Calendar.current.date(byAdding: .second, value: 1, to: midnight)
This works even if midnight doesn't exist due to daylight saving change.
Date is not a "date" in any meaningful way. It's a specific point in time, independent of any calendar or location. What you want to express is a point on a calendar: "one second" past an arbitrary calendar point we call "midnight." That's done with DateComponents.
var dc = DateComponents()
dc.hour = 0
dc.minute = 0
dc.second = 1
This is the second second of the first minute of the first hour (00:00:01) of an arbitrary day on an arbitrary calendar, which is what you've described.
More precisely, it's "zero hours, zero minutes, and one second," which is only "one second after midnight" if you add it to some "midnight." But beyond that, there is no independent "time" type. Those things only have meaning when applied to a Calendar.
(Keep in mind that due to DST change in some parts of world, such as Iran, there are sometimes two midnights in the same day. So when you ask for this kind of thing, you need to be very clear what you mean. Do you want every second after midnight or just the first one on a given day?)
I am not sure what I am missing here. I have Date object in swift, printed the description out and it is: "2000-01-01 07:00:00 +0000"
let formatter = DateFormatter()
formatter.dateFormat = "h:mm"
let durationDate = DateHelper.durationFormatter.date(from: “07:00”)!
let hours = Calendar.current.component(.hour, from: durationDate)
For some odd reason the hours is coming out as 0. Not sure what I am missing here?
Looks like I didn't take in account timezone. I am in +7 hours to UTC so it worked out perfectly to be zero.
This question already has answers here:
Swift convert unix time to date and time
(13 answers)
Closed 6 years ago.
I am retrieving information from an api and I am retrieving a value which holds a time, but the time looks like this, 1469880000 I did some research and realized that this is Epoch time. Now I wonder if you can make a mathmatical equation to convert Epoch to regular time. In there a function or framework in swift to so this? I want to turn this, 1469880000 into this H:MM -- .
As Alan notes you can use the overloaded method, here's an example.
let date = NSDate(timeIntervalSince1970: 1469880000)
Then to form it you can do something like the following...
let formatString = "yyyy-MM-dd'T'HH:mm:ss.sss'Z'"
func convertTimestampToUTC(inDate:NSDate,formatString:String) -> String{
let formatter = NSDateFormatter()
formatter.dateFormat = formatString
formatter.timeZone = NSTimeZone(abbreviation: "UTC")
let outDate = formatter.stringFromDate(inDate)
return outDate
}
Then use it like so,
let myFormattedDate = convertTimestampToUTC(date, formatString:formatString)
I'll update this shortly with some sample output once I get a moment, hope this helps!
NSDate has a init overload method that works with EPOCH time.
import Foundation
let myDate = NSDate.init(timeIntervalSince1970: 1469880000)
print(myDate)
I know there is several question about this. But i tried all the recommendation and still the time doesn't match the time atonmy real device.
let dater = NSDate()
let dayTimePeriodFormatter = NSDateFormatter()
dayTimePeriodFormatter.locale = NSLocale.currentLocale()
dayTimePeriodFormatter.timeZone = NSTimeZone(name: "UTC")
dayTimePeriodFormatter.dateFormat = "H:s"
var dateString = dayTimePeriodFormatter.stringFromDate(dater)
Device(real) time : 22:11 . dateString output : 00:36
Any suggestions? Thanks!
As Nick points out, you're using UTC as your time zone, and you can get to the local time zone with NSTimeZone.systemTimeZone()
It's also likely that you don't really intend to use just the hours (H) and seconds (s) You probably also want the minutes (m). Try using a dateFormat of "H:m:s"
Better yet, use "H:mm:ss" which will force the minutes and seconds to be two digits, as opposed to just one, so you don't wind up with "10:4:3"
Have you tried dayTimePeriodFormatter.dateFormat = "HH:mm"?
Apple uses fixed Internet. Check out here to see some examples. This may be helpful
All i need to do is to change the way that xcode displaying my values in. My code currently displaying the age of something in minutes e.g 4503mint . I want to be able to display these values in the following format 3 days 3 hours 3 mint rather than 4503 mint. Really appreciate your help. Regards
You said:
My code currently displaying the age of something in minutes e.g 4503mint . I want to be able to display these values in the following format 3 days 3 hours 3 mint rather than 4503 mint.
You can format this using NSDateComponentsFormatter. Just multiply the number of minutes by 60 to get the NSTimeInterval, and then you can supply that to the formatter:
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Full
let minutes = 4503
let timeInterval = NSTimeInterval(minutes * 60)
print(formatter.stringFromTimeInterval(timeInterval))
That will produce "3 days, 3 hours, 3 minutes" (or in whatever format appropriate for the locale for that device).
See NSDateComponentsFormatter Reference for more information.
You can obviously calculate it yourself (i.e. days = seconds/(3600*24)etc.), you can also look at NSDateComponentsFormatter, which may be exactly the functionality you are looking for with almost no coding effort.
You could calculate it yourself or do something like this:
let mySeconds = 700000
let date = NSDate(timeIntervalSinceNow: mySeconds) // difference to now
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = NSDateFormatter.dateFormatFromTemplate("yyyy.MM.dd", options: 0, locale: NSLocale.currentLocale())
dateFormatter.stringFromDate(date)