Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

This will give you a long date:

objDate = DateTime.Parse("YourInputValue");
Console.Write(objDate.ToLongDateString());

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

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted

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:

 

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:

Never trouble another for what you can do for yourself.
Posted

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.

Posted

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:

 

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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...