Convert DateTimeOffset to Date format in aspnet core

sblb 1,231 Reputation points
2022-06-20T19:28:58.027+00:00

Hi,

In the class FilePath I've a class

   public class FilePath  
    {  
     [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy/MM/dd}")]  
         public DateTimeOffset Created { get; set; }  
    }  

This class is associated to the view [dbo].[FilePath]

213038-image.png

The table is rendering with ajax, and I would like to change the format of the date?
213049-image.png

What is a best way to format the date in ajax or directly in the class?
But when I tried to put an annotation to format the date directly in class, it didn't take into account. Do you have any suggestion?

Thanks in advance !

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,594 questions
{count} votes

Accepted answer
  1. Xinran Shen - MSFT 2,091 Reputation points
    2022-06-21T03:03:32.71+00:00

    Hi @sblb ,
    The format of Created is the default format of DateTimeOffset after be serialized, SO if you want to Convert DateTimeOffset to DateTime, You can use the following generic method to Convert DateTimeOffset to Date in class.

    The following example defines a method named ConvertFromDateTimeOffset that converts DateTimeOffset values to DateTime values. Based on its offset, it determines whether the DateTimeOffset value is a UTC time, a local time, or some other time, and defines the returned date and time value's Kind property accordingly.

    public static class Change  
        {  
            public static DateTime ConvertFromDateTimeOffset(DateTimeOffset dateTime)  
            {  
                if (dateTime.Offset.Equals(TimeSpan.Zero))  
                    return dateTime.UtcDateTime;  
                else if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime)))  
                    return DateTime.SpecifyKind(dateTime.DateTime, DateTimeKind.Local);  
                else  
                    return dateTime.DateTime;  
            }  
        }  
    

    After you submit the table, You can Convert DateTimeOffset to Date in your action, Then convert it into the time format you want and store it in the database.

    [HttpPost]  
            public IActionResult Privacy(FilePath model)  
            {  
                 //........  
                var dateTime = Change.ConvertFromDateTimeOffset(model.Created);  
      
                // your other code  
      
            }  
    

    213196-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.

    Best regards,
    Xinran Shen


1 additional answer

Sort by: Most helpful
  1. SurferOnWww 3,066 Reputation points
    2022-06-21T00:59:39.933+00:00

    When the DateTime object is serialized to the JSON string by the Newtonsoft.Json (Core 2.x or earlier) or System.Text.Json (Core 3.x or later), the serialized string results in the format of "2017-08-26T15:39:32.6330349+09:00" which is the same as your "Created" result.

    You say:

    The table is rendering with ajax

    Therefore I guess that there is such serialization process somewhere between the database and your "Created" result.

    0 comments No comments

Your answer

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