0

I am pretty new to Entity Framework, I was able to create a process to add new entry to a database. However the last attribute in SQL Server is a datetime type. When I fill it using from the C# application it only includes the year month and day but not the time (cf the second entry below)

newEntry.TimeStamp = DateTime.Today;

(new entry is an instance of one of the class created by Entity Framework).

enter image description here

My initial idea was to change the format using :

string sqlFormatedDate = DateTime.Today.ToString("yyyy-MM-dd HH:mm:ss");

But obviously the application does not accept the type string when I get to the line :

newEntry.TimeStamp = sqlFormatedDate;

I get the error

Cannot Implicitly convert type 'string' to 'System.DateTime?'

How can one create a datetime object in C# that will properly be displayed in SQL Server?

EDIT:

Turns out one just has to use Datetime.now

1
  • 2
    what's wrong with DateTime.Now ?? Commented Mar 11, 2015 at 21:20

2 Answers 2

2

DateTime.Today is only the Date portion of the DateTime, the time is set to midnight. I think what you need is DateTime.Now.

Sign up to request clarification or add additional context in comments.

Comments

1

https://msdn.microsoft.com/en-us/library/system.datetime.today(v=vs.110).aspx

Property Value
Type: System.DateTime
An object that is set to today's date, with the time component set to 00:00:00.

Use DateTime.Now instead which will return date with time part.

1 Comment

Will check your answer in 10 min, when stack let me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.