© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•16mo ago•
5 replies
SWEETPONY

✅ How to save to database with ISO format?

Hello, I'd like to save datetime with timezone to database. I use NodaTime for this. Let's look into my code:

BaseEntity:
public class Entity
{
    public required Guid Id { get; set; }

    public LocalDateTime CreatedAt { get; protected set; }
    
    public DateTimeZone CreatedAtTz { get; protected set; }

    protected Entity()
    {
        Id = Guid.NewGuid();
        CreatedAt = LocalDateTime.FromDateTime(DateTime.UtcNow);
        CreatedAtTz = SystemClock.Instance.GetTimeZone();
    }
}
public class Entity
{
    public required Guid Id { get; set; }

    public LocalDateTime CreatedAt { get; protected set; }
    
    public DateTimeZone CreatedAtTz { get; protected set; }

    protected Entity()
    {
        Id = Guid.NewGuid();
        CreatedAt = LocalDateTime.FromDateTime(DateTime.UtcNow);
        CreatedAtTz = SystemClock.Instance.GetTimeZone();
    }
}


GetTimeZone
GetTimeZone
extension to automatically get time zone:
public static class DateTimeExtensions
{
    public static DateTimeZone GetTimeZone(this SystemClock systemClock)
    {
        var instance = systemClock.GetCurrentInstant();
        var zone = DateTimeZoneProviders.Bcl.GetSystemDefault();
        
        return instance.InZone(zone).Zone;
    }
}
public static class DateTimeExtensions
{
    public static DateTimeZone GetTimeZone(this SystemClock systemClock)
    {
        var instance = systemClock.GetCurrentInstant();
        var zone = DateTimeZoneProviders.Bcl.GetSystemDefault();
        
        return instance.InZone(zone).Zone;
    }
}


It works good. I see almost correctly filled date and time zone in database when user creates entity.

CreatedAt
CreatedAt
should be in
ISO
ISO
format but it looks not like
ISO
ISO
format in database
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

How to save signalR chat messages to database
C#CC# / help
11mo ago
❔ How to format float to currency
C#CC# / help
4y ago
❔ Need help to save data in to my Database
C#CC# / help
3y ago