قالب وردپرس درنا توس
Home / IOS Development / # 287: Relative Date Strings with DateComponents 📆 – Little Bites of Cocoa

# 287: Relative Date Strings with DateComponents 📆 – Little Bites of Cocoa



Subjects

Topics

In Bite # 286 we looked at the basic use of DateFormatter s to transform Date s to String p.

I Today we will explore another common mode of use at Date Say Our Apps: "Relative" Data Strings .

Think "2 days ago" "10 minutes ago" "Yesterday" and "Just now" . It represents time stamps in this way has become commonplace and it can help our users understand how old a content is.

Let's add this functionality to our app by expanding Date to give it a new estimated property to do this.

We start by adding a new file to our Xcode Project . We call it Date + Relative.swift . Type + SomeExtensionName The convention is borrowed from Objective-C .

Then we will set up our extension :

  extension   ]    {1
9659015] was
relativeFormatted : String { // TODO } }

Nice. Needs a way to calculate how much time it is between now and even (from the extended Date s point of view.) We could make a bunch of ugly maths, but we have a better way!

This is an amazing mode of use for the Foundation's DateComponents type. It has a function that can calculate the difference between two Date s and give it to us in a nicely (pre-calculated ) components like days, months, hours, seconds, etc.

We start our implementation like this:

  leave [19659014] now    =    Date [19659015] () 

  With    Components    =    Calendar .   by circulation .   dateComponents  [
   [. [19659034] year   .   month   .   weekOfYear   . [19659034]     .   time.               Other ] 
    from . Now  
    to [19659015]:    
] 

We ask the current Calendar to calculate the amount of each of the components we passed in between ] Date We submitted as from the argument to Date we passed the argument to .

Beautiful. Now it has returned the DateComponents type a bunch of properties that we will use to build our relatively formatted date `String.

Finally, we will inspect each computer component (starting with the widest at the top) and return a nicely formatted
String :

  if    la    years    =    components .   year     year  >    0    {
    return       (19659034] year )    year   ]   year    = =    1       ""  :    "? S" )    since "
} 

 " ]    months     months  >    0    {
    
    
    
    return [19659073] "   {19459044] months )    month    {  months    ==    1      " " :    "? S" [19659085])    since "
} 

  if    With    weeks    =    Components .   weekOfYear     weeks [19659026]>    0    {
    return    "[]  19659085 (  weeks )    weeks [19659074] " "    "? S" )    since "
}"      19659136] if    With    days    =    the components .    days  days   days  >    0    {
    cover    days  >    1 [19659066] otherwise    {   return    "going"  } 

    return    "   (  days )    day    (  days    ==    1       ""  :    "? S" )    since "
} 

  if    [la] 19659014 hours    19659014 hours    ]    the components .   hours  [19659062] hours  >    0    {
    return    «   (  hours ]    hours     hours [19659026] ==    1       ""  :    "? S" ) [19659077] since 
} 

  if    la [19659014] minute    =    the components .   minutes     minutes  >    0    {
    return    "   (  minutes )    minutes   minutes   ]   minutes    ==    1       ""  :    "? S" )    since "
} 

 " ]          seconds    =    components .   second     seconds [19659026]>    30    {
    return [19659073] "    seconds )    second     seconds    ==    1      " " :    "? S" )    a go 
} 

  return    "right now" 

Doing things manually as this allows us to customize and control accurately how this relative data string appears in our app.

This is pretty straightforward. We get a small fancy for the "days" component, which allows "yesterday" . We can also easily add support for future dates (that is, "tomorrow" ) here too.

All that's left to do is test it out. We can make it easy by building some date s earlier, then print into our new property :

  Date  (   : ] .   relativeFormatted 
  // "Right Now" 

  Date  (  timeIntervalSinceNow :    12 ] .   relativeFormatted 
  // "Right Now" 

  Date  (  timeIntervalSinceNow :    58 )  19659034] relativeFormatted 
  // 19 seconds ago "

 ]  

  

  

  //" 19 minutes ago "

 ]   timeIntervalSinceNow :    123  19659026] 

  

  

  

  

  ["20minutesago"

  Date  (]  [] [] :    1234  19659263 ] timeIntervalSinceNow : [1 9659081] 12345 ) .   relativeFormatted 
  // "3 hours ago" 

  Date  (  timeIntervalSinceNow :    123456 ) [19659033].   relativeFormatted 
  // "yesterday" 

  Date  (  timeIntervalSinceNow :    1234567 ) .   relative Formatted 
  // 19659015]  . 

  

  

  

     

]  ]  ]  19659029] 

  

  

  

    ]   

  19659017] 

  .   RelativFormatted 
  // "3 years ago" 

  Date  19659290] timeIntervalSinceNow :    1234568901 ) 
  . [19659034] relativeFormatted    // "39 years ago" 

  Date  (  timeIntervalSinceNow : [19659081] 12345689012 ) [19659384].   relativeFormatted    // "391 years ago" 

Source link