-2

I am making my own app, which involves uploading pictures and the date in which the post was made is necessary. I was wondering how I can upload this data to parse, and then how to retrieve it from parse to populate it in a label.

Best regards

1 Answers1

0

You will first ask your backend that in which format he needs the date. Suppose he says "yyyy-MM-dd'T'HH:mm:ss.000Z"

Then you will do this

  let date = Date()

this will give you current date object there are other ways too you can take date object for eg. date picker. Then

  let dateFormatter = DateFormatter()
  dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.000Z"
  let dateString = dateFormatter.string(from: date!)

now in dateString you'll get the date in string which you will send and at the time of receiving you'll again get date in String in some specific format to convert that string to Date Object do the following. Suppose dateData is the var in which you stored the dateString received from the API.

  dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.000Z" // put here the date format told by your backend developer
  let date = dateFormatter.date(from: dateData)
sanjaykmwt
  • 586
  • 3
  • 19
  • but how do I store this data in Parse? For example, I have a class name Post, can I store this data in the "Post" class and then populate it to a label in the home section where the images will be? – Ximena Flores de la Tijera Jun 08 '18 at 05:44
  • inWhich Format do you want to populate it – sanjaykmwt Jun 08 '18 at 05:46
  • Don't do that, Instead send the UTC timestamp to server which can easily convertible to any time zone later when you will show it at client end. – TheTiger Jun 08 '18 at 08:26
  • @sanjaykmwt My comment was not for you so be cool. I was just helping the OP in simpler way. Why he should send the date object in `String` format and remember the format to make the `Date` object back from that `String` and I don't argue with too much intelligent people. – TheTiger Jun 08 '18 at 09:32
  • No no I am not arguing bro, but need to know how you'll do it without dateFormats? – sanjaykmwt Jun 08 '18 at 09:46
  • @sanjaykmwt UTC timestamp is a number. `let timeInterval = date.timeIntervalSince1970` will give you the UTC timestamp and you can make the date back by `let date = Date(timeIntervalSince1970: timeInterval)`. `DateFormat` will make the things complex while there is no need to use it. We use date formater for client side to parsing the date in desired format. – TheTiger Jun 08 '18 at 10:59
  • could some one explain it more thoroughly through an email? I'm kind of lost hahaha. sanjaykmwt - what type of formats are there? I've just coded for one year and am still learning a lot of things. – Ximena Flores de la Tijera Jun 08 '18 at 21:30