32

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?

Dharmesh
  • 132
  • 1
  • 12
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

9 Answers9

43
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;
}
thomas
  • 1,399
  • 1
  • 17
  • 32
16
 DateTime d = default(DateTime);

The default keyword works for all data types too!

Lai
  • 1,320
  • 2
  • 13
  • 24
9

Both are same....

1

mySmallVuln.Published = new DateTime(1998,04,30,0,0,0);
mySmallVuln.LastUpdated = new DateTime(2007,11,05,0,0,0);

2

mySmallVuln.Published = new DateTime(1998,04,30);
mySmallVuln.LastUpdated = new DateTime(2007,11,05);

in the first method you can assign hour minute and second respectively in parameter at the last three parameter.

Shell
  • 6,818
  • 11
  • 39
  • 70
4

To initialize a DateTime value you can use the DateTime constructor:

mySmallVuln.Published = new DateTime(1998,04,30);
Neil Knight
  • 47,437
  • 25
  • 129
  • 188
4

You are using a character literal '' which can only contain one character. If you want to use a string literal use "" instead.

C# does not support DateTime-literals as opposed to VB.NET (#4/30/1998#).

Apart from that, a string is not a DateTime. If you have a string you need to parse it to DateTime first:

string published = "1998,04,30";
DateTime dtPublished = DateTime.ParseExact(published, "yyyy,MM,dd", CultureInfo.InvariantCulture);
mySmallVuln.Published = dtPublished; 

or you can create a DateTime via constructor:

DateTime dtPublished = new DateTime(1998, 04, 30);

or, since your string contains the year, month and day as strings, using String.Split and int.Parse:

string[] tokens = published.Split(',');
if (tokens.Length == 3 && tokens.All(t => t.All(Char.IsDigit)))
{
    int year = int.Parse(tokens[0]);
    int month = int.Parse(tokens[1]);
    int day = int.Parse(tokens[2]);
    dtPublished = new DateTime(year, month, day);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
3

Unfortunately, C# does not support Date literals. You should change your code and instantiate the DateTime objects using new DateTime(..), as thomas exemplified in his answer.

Just for the sake of expanding on this topic: VB.NET supports date literals using the # character (ironically). This is how they can be expressed, from the MSDN documentation:

Dim d As Date
d = # 8/23/1970 3:45:39AM #
d = # 8/23/1970 #              ' Date value: 8/23/1970 12:00:00AM.
d = # 3:45:39AM #              ' Date value: 1/1/1 3:45:39AM.
d = # 3:45:39 #                ' Date value: 1/1/1 3:45:39AM.
d = # 13:45:39 #               ' Date value: 1/1/1 1:45:39PM.
d = # 1AM #                    ' Date value: 1/1/1 1:00:00AM.
d = # 13:45:39PM #             ' This date value is not valid.
rla4
  • 1,228
  • 13
  • 25
3

If you search for the error you get, you'll find:

This is because, in C#, single quotes ('') denote (or encapsulate) a single character, whereas double quotes ("") are used for a string of characters.

So you'll try:

DateTime foo = "2014,02,20";

Which yields:

Cannot implicitly convert type 'string' to 'System.DateTime'

Now if you search for that error, you'll find:

int StartYear = 2012;
int StartMonth = 06;
int StartDay = 15;

DateTime dt = new DateTime(StartYear, StartMonth, StartDay);
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1

use this

// year,month,day

mySmallVuln.Published=new DateTime(2011,11,4);
gericooper
  • 242
  • 2
  • 11
0

You can either parse a string, like yours. Or you can instansiate a DateTime object with numbers.

DateTime date1 = new DateTime(1998, 04, 30);
DateTime date2 = DateTime.ParseExact("1998,04,30", "yyyy,MM,dd", CultureInfo.InvariantCulture);
Terje
  • 1,753
  • 10
  • 13