Datetime

swteam

Newcomer
Joined
Jun 23, 2008
Messages
9
hai all,

i have to send date as long formate my customer having one application which is developed in java it requires the input date as long i have the function when i run the program which in java i am getting a longnumeric value then i found it shows current date as milliseconds from 1970 jan 1 so when i am sending i also want to send it in the same way so please help me how to do it
 
This will give you a long date:
C#:
objDate = DateTime.Parse("YourInputValue");
Console.Write(objDate.ToLongDateString());

// or this for current
Console.Write(DateTime.Now.ToLongDateString());
 
ToUnixTime!

Actually, I think what you are looking for is a function to convert a DateTime to a UNIX file time (milliseconds since 1st Jan 1970). Off the top of my head, such a function might look like this:

C#:
public long ToUnixTime(DateTime dateTime)
{
    return (dateTime.Ticks - new DateTime(1970, 1, 1).Ticks) / 10000;
}

Ticks are 100-nanosecond intervals, so dividing by 10000 gives milliseconds.

Good luck :cool:
 
Re: ToUnixTime!

I would think that this

dateTime.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;

would be equivalent to what you've posted, MrPaul, but when I run the code side by side I get drastically different values. I don't have time to investigate it now so if someone could sort it out, I am curious to know why.
 
Returns the same result

I have tested this and both return the same value, so I don't know why you're seeing different results:

C#:
static void Main(string[] args)
{
    DateTime now = DateTime.Now;

    Console.WriteLine("1: {0}", ToUnixTime1(now));
    Console.WriteLine("2: {0}", ToUnixTime2(now));
    Console.ReadLine();
}

public static long ToUnixTime1(DateTime dateTime)
{
    return (dateTime.Ticks - new DateTime(1970, 1, 1).Ticks) / 10000;
}

public static long ToUnixTime2(DateTime dateTime)
{
    return (long) dateTime.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
}

Output:


1: 1216074919320
2: 1216074919320


:confused:
 
Back
Top