Asp.net mvc 5 add timestamp when things were added?

Let's say I have a comments section on my website that is stored in a database. When I add a new comment, I would like to see who added it and what date / time they posted it.

I donโ€™t know how Iโ€™ll go further and do it. Anyone can nudge me towards the right direction?

I know I can do it. public DateTime Time { get; set; }

Anyway, when the user enters their own date, I need to be automatic.

Here is a model I tried that doesn't compile but instead generates Error 3 The type name 'Now' does not exist in the type 'System.DateTime'

:

public class Suggestion {
    public int Id { get; set; } 
    public string Comment { get; set; } 
    public DateTime.Now Time { get; set; } 
}

      

And this is the error I am getting Error 3 The type name 'Now' does not exist in the type 'System.DateTime'

+3


source to share


5 answers


If you want it to start automatically and every time, you must set WhenCreated

in the constructor. This way you don't have to forget anything to install it anywhere.

public class Suggestion 
{
  public DateTime WhenCreated { get; set; }
  /* other props */

  public Suggestion() 
  {
    WhenCreated = DateTime.Now;
  }
}

      



When rehydrating Suggestion

from a record, the database WhenCreated

will be updated with EntityFramework or whatever persistence tier you use. This happens after the call to Constructor, so whatever initial value you have is irrelevant. When your application creates a new one Suggestion

, the field WhenCreated

will automatically be set to "Now".

Note: DateTime.Now

Returns the current date and time of the server time zone. You might have to handle translating to local timezones for your users, and if so it would be useful to use DateTime.UtcNow

to get UTC time which will be easier to localize in the future (won't double / play the hour during daylight saving time)

+5


source


public class Suggestion {
  public int Id { get; set; } 
  public string Comment { get; set; } 
  public DateTime Time { get; set; } 
}

Suggestion s = new Suggestion();
s.Time = DateTime.Now;

      



+1


source


Two possible options:

  • When you define a table to store comments in the database, add a new column to store the date and time the row was created. Give it the default. For example, on Sql server, you can use getdate () by default. The upside to this method is that you don't need to write additional C # code to set the value in the WhenCreated column:

...

CREATE TABLE [dbo].[Coments](
   [Id] [int] NULL,
   [Comment] [nvarchar](50) NULL,
   [WhenCreated] [datetime2](7) NOT NULL DEFAULT getdate()
)

      

  1. If you want to do it in C # you can just use DateTime.Now

...

 public class Suggestion {
        public int Id { get; set; } 
        public string Comment { get; set; } 
        public DateTime WhenCreated { get; set; } 
    }

    var n = new Suggestion();
    n.WhenCreated = DateTime.Now;

      

0


source


I solved this problem like this

 public class Time
{
    private DateTime _date = DateTime.Now;

    public int ID { get; set; }

    public DateTime DateCreated
    {
        get { return _date; }
        set { _date = value; }
    }

}

      

0


source


You have the correct idea of โ€‹โ€‹what you want to do, you just donโ€™t know how to do it.

If you have a value that you want to set when creating an object, then the default constructor for objects is the ideal place to do so.

The default constructor is called by the program every time an object is created by calling a new keyword.

So back to your question, to accomplish what you want to do, you just need to do something like this:

public class YourClass() 
{
    public DateTime CreatedAt { get; set; }

    public YourClass()
    {
        CreatedAt = DateTime.Now;    
    }
}

      

Happy trails!

0


source







All Articles