swteam Posted July 3, 2008 Posted July 3, 2008 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 Quote
Nate Bross Posted July 3, 2008 Posted July 3, 2008 This will give you a long date: objDate = DateTime.Parse("YourInputValue"); Console.Write(objDate.ToLongDateString()); // or this for current Console.Write(DateTime.Now.ToLongDateString()); Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
MrPaul Posted July 3, 2008 Posted July 3, 2008 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: Quote Never trouble another for what you can do for yourself.
mskeel Posted July 10, 2008 Posted July 10, 2008 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. Quote
MrPaul Posted July 14, 2008 Posted July 14, 2008 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: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.