Format published_date

Jassim Al Rahma 1,521 Reputation points
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

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,356 questions
C#
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.
10,221 questions
.NET Runtime
.NET Runtime
.NET: Microsoft Technologies based on the .NET software framework.Runtime: An environment required to run apps that aren't compiled to machine language.
1,118 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,261 Reputation points Microsoft Vendor
    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.