Swift How to Convert Parse Createdat Time to Time Ago

Swift How to convert Parse createdAt time to time ago?

You can use NSCalendar isDateInToday to check if createdAt date is in same day as today, and use isDateInYesterday to check if it was yesterday. Just add a conditional and return a custom string for those conditions, for all other conditions just let date components formatter take care of it for you.

extension Formatter {
static let time: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .gregorian)
formatter.dateFormat = "h:mm a"
return formatter
}()
static let dateComponents: DateComponentsFormatter = {
let formatter = DateComponentsFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.unitsStyle = .full
formatter.maximumUnitCount = 1
formatter.zeroFormattingBehavior = .default
formatter.allowsFractionalUnits = false
formatter.allowedUnits = [.year, .month, .weekOfMonth, .day, .hour, .minute, .second]
return formatter
}()
}

extension Date {

var time: String { return Formatter.time.string(from: self) }

var year: Int { return Calendar.autoupdatingCurrent.component(.year, from: self) }
var month: Int { return Calendar.autoupdatingCurrent.component(.month, from: self) }
var day: Int { return Calendar.autoupdatingCurrent.component(.day, from: self) }

var elapsedTime: String {
if timeIntervalSinceNow > -60.0 { return "Just Now" }
if isInToday { return "Today at \(time)" }
if isInYesterday { return "Yesterday at \(time)" }
return (Formatter.dateComponents.string(from: Date().timeIntervalSince(self)) ?? "") + " ago"
}
var isInToday: Bool {
return Calendar.autoupdatingCurrent.isDateInToday(self)
}
var isInYesterday: Bool {
return Calendar.autoupdatingCurrent.isDateInYesterday(self)
}
}

testing:

Calendar.autoupdatingCurrent.date(byAdding: .second, value: -59, to: Date())!
.elapsedTime // "Just Now"

Calendar.autoupdatingCurrent.date(byAdding: .minute, value: -1, to: Date())!
.elapsedTime // "Today at 5:03 PM"
Calendar.autoupdatingCurrent.date(byAdding: .hour, value: -1, to: Date())!
.elapsedTime // "Today at 4:04 PM"

Calendar.autoupdatingCurrent.date(byAdding: .day, value: -1, to: Date())!
.elapsedTime // "Yesterday at 5:02 PM"
Calendar.autoupdatingCurrent.date(byAdding: .weekOfYear, value: -1, to: Date())!
.elapsedTime // "1 week ago"

Calendar.autoupdatingCurrent.date(byAdding: .month, value: -2, to: Date())!
.elapsedTime // "2 months ago"
Calendar.autoupdatingCurrent.date(byAdding: .year, value: -1, to: Date())!
.elapsedTime // "1 year ago"

Swift convert time to time ago

I'll just update the Truongky's answer for Swif 3:

extension Date {

func getElapsedInterval() -> String {

let interval = Calendar.current.dateComponents([.year, .month, .day], from: self, to: Date())

if let year = interval.year, year > 0 {
return year == 1 ? "\(year)" + " " + "year ago" :
"\(year)" + " " + "years ago"
} else if let month = interval.month, month > 0 {
return month == 1 ? "\(month)" + " " + "month ago" :
"\(month)" + " " + "months ago"
} else if let day = interval.day, day > 0 {
return day == 1 ? "\(day)" + " " + "day ago" :
"\(day)" + " " + "days ago"
} else {
return "a moment ago"

}

}
}

If you prefer a localizable response instead of only english this code will do the work

extension Date {
func getElapsedInterval() -> String {

var calendar = Calendar.current
calendar.locale = Locale(identifier: Bundle.main.preferredLocalizations[0])
// IF THE USER HAVE THE PHONE IN SPANISH BUT YOUR APP ONLY SUPPORTS I.E. ENGLISH AND GERMAN
// WE SHOULD CHANGE THE LOCALE OF THE FORMATTER TO THE PREFERRED ONE
// (IS THE LOCALE THAT THE USER IS SEEING THE APP), IF NOT, THIS ELAPSED TIME
// IS GOING TO APPEAR IN SPANISH

let formatter = DateComponentsFormatter()
formatter.unitsStyle = .full
formatter.maximumUnitCount = 1
formatter.calendar = calendar

var dateString: String?

let interval = calendar.dateComponents([.year, .month, .weekOfYear, .day], from: self, to: Date())

if let year = interval.year, year > 0 {
formatter.allowedUnits = [.year] //2 years
} else if let month = interval.month, month > 0 {
formatter.allowedUnits = [.month] //1 month
} else if let week = interval.weekOfYear, week > 0 {
formatter.allowedUnits = [.weekOfMonth] //3 weeks
} else if let day = interval.day, day > 0 {
formatter.allowedUnits = [.day] // 6 days
} else {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: Bundle.main.preferredLocalizations[0]) //--> IF THE USER HAVE THE PHONE IN SPANISH BUT YOUR APP ONLY SUPPORTS I.E. ENGLISH AND GERMAN WE SHOULD CHANGE THE LOCALE OF THE FORMATTER TO THE PREFERRED ONE (IS THE LOCALE THAT THE USER IS SEEING THE APP), IF NOT, THIS ELAPSED TIME IS GOING TO APPEAR IN SPANISH
dateFormatter.dateStyle = .medium
dateFormatter.doesRelativeDateFormatting = true

dateString = dateFormatter.string(from: self) // IS GOING TO SHOW 'TODAY'
}

if dateString == nil {
dateString = formatter.string(from: self, to: Date())
}

return dateString!
}

Swift display time ago from Date (NSDate)

If you just want a Time Ago extension for Date go to the bottom of the answer /p>

I'll show you an example just to get seconds ago and after I'll show your extension updated.

Note: you can use directly the date from Pase if you want:

if let pastDate = (object?["createdAt"] as? Date) {
cell.TimeAgo.text = pastDate.timeAgoDisplay()
}

Since Swift 5.1

Example how to display seconds ago with Swift 5.1:

Since iOS13 Apple introduce a new class RelativeDateTimeFormatter

extension Date {
func timeAgoDisplay() -> String {
let formatter = RelativeDateTimeFormatter()
formatter.unitsStyle = .full
return formatter.localizedString(for: self, relativeTo: Date())
}
}

This class will allow you to get a time ago string based on your language. It automatically select the right unit of time based on your interval, here is an example:

|--------------------------|------------------|
| Time interval in seconds | Display |
|--------------------------|------------------|
| -6 | 6 seconds ago |
| -60 | 1 minute ago |
| -600 | 10 minutes ago |
| -6000 | 1 hour ago |
| -60000 | 16 hours ago |
|--------------------------|------------------|

You'll notice that it handle automatically plurals for you.

Swift 3 or Swift 4

Example how to get seconds ago with Swift 3 or Swift 4:

First: To get the number of seconds ago we need to check if we have one minutes or less, to get the current Date minus one minute you can write that:

let minuteAgo = calendar.date(byAdding: .minute, value: -1, to: Date())!

Second: Now compare the 2 dates! (In the case of your extension we replace yourDate by self) and get the difference between this 2 dates.

if (minuteAgo < yourDate) {
let diff = Calendar.current.dateComponents([.second], from: yourDate, to: Date()).second ?? 0
print("\(diff) sec ago")
}

That's all, now you can print the time ago !

So your extension is like this:
(This is a simple extension to get the time ago)

extension Date {
func timeAgoDisplay() -> String {

let calendar = Calendar.current
let minuteAgo = calendar.date(byAdding: .minute, value: -1, to: Date())!
let hourAgo = calendar.date(byAdding: .hour, value: -1, to: Date())!
let dayAgo = calendar.date(byAdding: .day, value: -1, to: Date())!
let weekAgo = calendar.date(byAdding: .day, value: -7, to: Date())!

if minuteAgo < self {
let diff = Calendar.current.dateComponents([.second], from: self, to: Date()).second ?? 0
return "\(diff) sec ago"
} else if hourAgo < self {
let diff = Calendar.current.dateComponents([.minute], from: self, to: Date()).minute ?? 0
return "\(diff) min ago"
} else if dayAgo < self {
let diff = Calendar.current.dateComponents([.hour], from: self, to: Date()).hour ?? 0
return "\(diff) hrs ago"
} else if weekAgo < self {
let diff = Calendar.current.dateComponents([.day], from: self, to: Date()).day ?? 0
return "\(diff) days ago"
}
let diff = Calendar.current.dateComponents([.weekOfYear], from: self, to: Date()).weekOfYear ?? 0
return "\(diff) weeks ago"
}
}

To use it, this is very straightforward:

var now = Date()
now.timeAgoDisplay()

iOS Parse, Created X Time ago - Social

You can create Date From String using GMT Timezone and then compare [NSDate date] and converted date. You can use NSDateComponents and NSCalendarUnit. refer this How to convert NSDate in to relative format as "Today","Yesterday","a week ago","a month ago","a year ago"?

You can also use this its a good library.
https://github.com/kevinlawler/NSDate-TimeAgo

Hope this will help. but first you should convert string to date using GMT timezone then only you can compare both GMT dates. One is your current global time and second is your server global time. so it will give you same difference in any timezone. Hope you understand.

retrieving createdAt and updatedAt from Parse using Swift

Actually I've found the answer which is that the columns objectId, createdAt and updatedAt that are created automatically by Parse are properties and we have to call them alone and they can't be retrieved with the whole object.

I called the object "article" so I can get the createdAt like this

let theDate = article.createdAt

The type here will be Date? and if you want to embed it within the table view you have to convert to String.

Date to milliseconds and back to date in Swift

I don't understand why you're doing anything with strings...

extension Date {
var millisecondsSince1970:Int64 {
Int64((self.timeIntervalSince1970 * 1000.0).rounded())
}

init(milliseconds:Int64) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
}
}

Date().millisecondsSince1970 // 1476889390939
Date(milliseconds: 0) // "Dec 31, 1969, 4:00 PM" (PDT variant of 1970 UTC)

How can I convert string date to NSDate?

try this:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = /* find out and place date format from
* http://userguide.icu-project.org/formatparse/datetime
*/
let date = dateFormatter.dateFromString(/* your_date_string */)

For further query, check NSDateFormatter and DateFormatter classes of Foundation framework for Objective-C and Swift, respectively.

Swift 3 and later (Swift 4 included)

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = /* date_format_you_want_in_string from
* http://userguide.icu-project.org/formatparse/datetime
*/
guard let date = dateFormatter.date(from: /* your_date_string */) else {
fatalError("ERROR: Date conversion failed due to mismatched format.")
}

// use date constant here

Update the createdAt column in Parse.com

I am sorry, but the field createdAt and updatedAt cannot be modified manually; https://www.parse.com/questions/can-i-modify-the-field-createdat

Try something like this:

  1. Create a new column in Parse.com, named ex: modifiedAt as an NSDate.
  2. Change your code gameScore["createdAt"] to gameScore["modifiedAt"].

Now that field will be updated as the same format as createdAt, and you can easy update the column as you would like.



Related Topics



Leave a reply



Submit