B Best
B Best

Reputation: 1166

TimeSpan to UTC milliseconds

I currently have a TimeSpan variable that holds the time set from a TimePicker. I need a long similar to the one given by TimeSpan.TotalMilliseconds() but I need it in respect to UTC.

I saw some tactics like converting to a DateTime and then using DateTime.ToUniversalTime() but I haven't successfully gotten the total milliseconds from it after the conversion. I tried taking the new UTC DateTime and using DateTime.TimeOfDay() to put it back into a TimeSpan type and then use the TimeSpan.TotalMilliseconds() again but it did not work.

Any ideas?

Upvotes: 2

Views: 767

Answers (2)

Sunny
Sunny

Reputation: 1534

You can use this method

public double MilliTimeStamp(DateTime TheDate)
        {
            DateTime d1 = new DateTime(1970, 1, 1);
            DateTime d2 = TheDate.ToUniversalTime();
            TimeSpan ts = new TimeSpan(d2.Ticks - d1.Ticks);
            return ts.TotalMilliseconds;
        }

Upvotes: 2

Anuj Kumar
Anuj Kumar

Reputation: 88

I also had the same problem. Use this to your datepicker method-

DateTime.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
DateTime.set(final_year, final_month, final_day, final_hour, final_minute,0);
private long datetime=DateTime.getTimeInMillis();
String dt= String.valueOf(datetime);

I think this will solve your problem!

Upvotes: 0

Related Questions