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'
source to share
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)
source to share
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()
)
- 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;
source to share
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!
source to share