Share via

Format published_date

Anonymous
2021-07-12T22:26:33.12+00:00

Hi,

How can I format a published_date field just like Instagram and other social media this way:

[.....] second(s)
[.....] minute(s)
[.....] hour(s)
[.....] day(s)
[.....] week(s) [if it's 1 week or 2 weeks or 3 weeks only.

and if it's more than 30 days then:

dd MMM yyyy

Kindly help..

Thanks,
Jassim

Developer technologies | .NET | Xamarin
Developer technologies | .NET | .NET Runtime
Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2021-07-13T02:54:41.617+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Based on your description, I write a method, you can make a test.

       public string DateStringFromNow(DateTime dt)  
               {  
                   TimeSpan span = DateTime.Now - dt;  
                    
                   if (span.TotalDays > 30)  
                   {  
                         
                       return dt.ToString(" dd-MM-yyyy");   
                   }  
                   else  
                   if (span.TotalDays > 21)  
                   {  
                       return "3 weeks ago";  
                   }  
         
                   else  
                   if (span.TotalDays > 14)  
                   {  
                       return "2 weeks ago";  
                   }  
                   else  
                   if (span.TotalDays > 7)  
                   {  
                       return "1 week ago";  
                   }  
                   else  
                   if (span.TotalDays > 1)  
                   {  
                       return string.Format("{0}day ago", (int)Math.Floor(span.TotalDays));  
                   }  
                   else  
                   if (span.TotalHours > 1)  
                   {  
                       return string.Format("{0}hours ago", (int)Math.Floor(span.TotalHours));  
                   }  
                   else  
                   if (span.TotalMinutes > 1)  
                   {  
                       return string.Format("{0}minutes ago", (int)Math.Floor(span.TotalMinutes));  
                   }  
                   else  
                   if (span.TotalSeconds >= 1)  
                   {  
                       return string.Format("{0}seconds ago", (int)Math.Floor(span.TotalSeconds));  
                   }  
                   else  
                   {  
                       return "1second ago";  
                   }  
               }  
           }  
    

    You can test it with myLabel.Text = DateStringFromNow(new DateTime(2021, 6,12, 9, 15, 30)); in Xamarin forms.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.