3

I need to store DateTime in int. So I tried below codes

Int64 n = Int64.Parse(DateTime.Today.ToString("dd-MM-yyyy"));

or

Int64 twoday_date=Convert.ToInt64(System.DateTime.Today.ToString("dd-MM-yyyy"));

but its showing error:

Input string was not in a correct format.

Where is the error?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Happy
  • 1,105
  • 5
  • 17
  • 25

4 Answers4

8

Just use DateTime.Ticks instead - there's absolutely no reason to start converting to and from strings here.

long ticks = DateTime.Today.Ticks;

// Later in the code when you need a DateTime again
DateTime dateTime = new DateTime(ticks);

Note that this will use the local date - if you're trying to retain a global timestamp, you should use DateTime.UtcNow instead of DateTime.Today.

If you really need int instead of long, you probably ought to translate and scale, e.g. to seconds since the Unix epoch.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • how to compare with date time picker "int p_date = int.Parse(datetime_from.ToString());" – Happy Jan 11 '14 at 13:24
  • @Happy: I wouldn't even try. Don't try to parse a string representation of a date/time as an integer in the first place. – Jon Skeet Jan 11 '14 at 13:27
0

You could either store the milliseconds from a certain point in time (that you define), or you could use a format such as yyyyMMddhhmmss (and fff if you want more precision).

Sachin Nayak
  • 1,041
  • 9
  • 17
0

The original question asks where is the error? within:

Int64 n = Int64.Parse(DateTime.Today.ToString("dd-MM-yyyy"));

The ToString(...) method generates a string representation of the date time value. In this case its argument, the string "dd-MM-yyyy" gives the format of the string to be generated. So today that will generate the string "11-01-2014". The Int64.Parse(...) attempts to parse its argument string as an integer, but here it has a mix of digits and hyphens. Hence it throws an exception.

Understanding these sort of problems can be tricky. One technique is to break the statement into smaller pieces and understand each of them in turn. When the problem is resolved the corrected pieces can be assembled into a single statement if desired. In this case the statement could be split to be:

string s = DateTime.Today.ToString("dd-MM-yyyy");
Console.WriteLine("The date string is '{0}'", s);
Int64 n = Int64.Parse(s);

Then use either a debugger or the WriteLine shown to show the value in s. Note that the WriteLine encloses the displayed value of s in quotes so that the presence or absence of spaces, newlines and other unexpected characters can easily be detected.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
-2
// the local utc offset is  +2:00
 public class Program
{
    public static void Main(string[] args)
    {
       // code executed in timezone GMT+2:00
        long ticksUtc = DateTime.UtcNow.Ticks;

        Console.WriteLine("{0:d}",ticksUtc);
        DateTime _todayUtc = new DateTime(ticksUtc);
         Console.WriteLine("{0}",_todayUtc);
        // get local date time from Utc time
         Console.WriteLine("{0}",_todayUtc.ToLocalTime());
        Console.WriteLine();
        long ticksLocal = DateTime.Now.Ticks;
        Console.WriteLine("{0:d}",ticksLocal);
        Console.WriteLine("{0:d}",ticksLocal-ticksUtc);
       DateTime _todayLocal = new DateTime(ticksLocal);
         Console.WriteLine("{0}",_todayLocal);

        // get the utc time from _todaylocal time
        Console.WriteLine("{0}",_todayLocal.ToUniversalTime());
    }
}
  • 1
    This answer doesn't add anything useful to the 2014 one. Could you please edit your answer to add why you deemed it useful? – Giulio Caccin Aug 24 '17 at 08:51