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.