Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a DateTime field?

Tags:

c#

datetime

I am absolutly new in C# (I came from Java) and I have a very stupid problem

I have to initialize some DateTime fields into an object but I have some problems doing it.

In particular I am trying to inizialize these fields in this way:

mySmallVuln.Published = '1998,04,30'; mySmallVuln.LastUpdated = '2007,11,05'; 

But Visual Studio sign me it as error

Too many characters in character literal

What am I missing? How to solve it?

like image 412
AndreaNobili Avatar asked Feb 20 '14 11:02

AndreaNobili


People also ask

How do I set the empty DateTime in Python?

someDate = null; myCommand. Parameters. AddWithValue("@SurgeryDate", someDate);

What is default value for DateTime in C#?

The default and the lowest value of a DateTime object is January 1, 0001 00:00:00 (midnight). The maximum value can be December 31, 9999 11:59:59 P.M. Use different constructors of the DateTime struct to assign an initial value to a DateTime object.

How do I create a specific date in C#?

To set dates in C#, use DateTime class. The DateTime value is between 12:00:00 midnight, January 1, 0001 to 11:59:59 P.M., December 31, 9999 A.D. Let's create a DateTime object.

What is DateTime now in C#?

Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time. public: static property DateTime Now { DateTime get(); }; C# Copy.


2 Answers

mySmallVuln.Published = new DateTime(1998,04,30); 

Or perhaps like this

var date = DateTime.MinValue; if (DateTime.TryParse("1998/04/30", out date)) {     //Sucess...     mySmallVuln.Published = date; } 
like image 74
thomas Avatar answered Sep 25 '22 11:09

thomas


 DateTime d = default(DateTime); 

The default keyword works for all data types too!

like image 28
Lai Avatar answered Sep 26 '22 11:09

Lai