Convert Time With Milliseconds to Datetime in C#

Conversion from milliseconds to DateTime format

DateTime in .NET is initialized to 0001-01-01 00:00:00 and then you add your TimeSpan, which seems to be 45 Years.

It is common for such (milli)-second time definitions to start at 1970-01-01 00:00:00, so maybe the following gives you the expected result:

double ticks = double.Parse(startdatetime);
TimeSpan time = TimeSpan.FromMilliseconds(ticks);
DateTime startdate = new DateTime(1970, 1, 1) + time;

or simply

var date = (new DateTime(1970, 1, 1)).AddMilliseconds(double.Parse(startdatetime));

Convert a time in milliseconds to local date string

Since your ConvertToLocalDate function returns the date and time to your local time zone. You need to convert it to UTC to get the expected date and time.

class Program
{
static void Main(string[] args)
{
Console.WriteLine(ConvertToLocalDate("1579631400000").ToUniversalTime());

Console.ReadKey();
}

public static DateTime ConvertToLocalDate(string timeInMilliseconds)
{
double timeInTicks = double.Parse(timeInMilliseconds);
TimeSpan dateTimeSpan = TimeSpan.FromMilliseconds(timeInTicks);
DateTime dateAfterEpoch = new DateTime(1970, 1, 1) + dateTimeSpan;
DateTime dateInLocalTimeFormat = dateAfterEpoch.ToLocalTime();
return dateInLocalTimeFormat;
}
}

Or simply do not use ToLocalTime() inside your ConvertToLocalDate (if this is the case your function should not be named as ConvertToLocalDate)

How to convert Milliseconds to date format in C#?

Start by converting your milliseconds to a TimeSpan:

var time = TimeSpan.FromMilliseconds(milliseconds);

Now, in .NET 4 you can call .ToString() with a format string argument. See http://msdn.microsoft.com/en-us/library/system.timespan.tostring.aspx

In previous versions of .NET, you'll have to manually construct the formatted string from the TimeSpan's properties.

Not able to convert a string value to Date time including milliseconds

The DateTime.ParseExact should solve the formatting like this:

DateTime originalDate = DateTime.ParseExact(currentDate, "dd-MMM-yy hh.mm.ss.ffffff tt", CultureInfo.InvariantCulture, DateTimeStyles.None);

or if time part are separated with colons:

DateTime.ParseExact(currentDateWithColons, "dd-MMM-yy hh:mm:ss.ffffff tt", CultureInfo.InvariantCulture, DateTimeStyles.None);

Both dots and colons producing the same result using Console.WriteLine(originalDate.ToString("dd/MM/yyyy hh:mm:ss.ffffff tt")):

29/04/2017 03:42:06.410000 PM

Working example: .NET Fiddle Demo

How to convert string into date time with milliseconds in C#

Seems like you don't need substrings at all.

Just put your date string right into ParseExact and you will get what you want.

However the error there is because you have different milliseconds format: it expects you will put three digits there but you passed only two:

var dateTime = "03/04/2019 15:16:57.73";

var format = "dd/MM/yyyy HH:mm:ss.ff"; - this is the fix.

How to store a Date time with milliseconds into a DateTime Object in C#?

I want to store the Data time in 01/01/2008 00:30:45.125 format as a key to in Dictionary.

DateTime objects do not have formats. They are binary data that represent a date and time. You can easily use a DateTime object with a value that represents the instant described by 01/01/2008 00:30:45.125 as a dictionary key, but that's not the same thing.

If you need a particular string format, use a string as the key type. But probably you're overthinking this, and you really don't want that particular string format in the Dictionary. After all, you can always take that DateTime object and format it for display later on, and that's really the better practice.

The remaining concern is DateTime has sub-millisecond precision, meaning you can have more than one DateTime value in a single millisecond. If it's possible for your environment to produce two data points within that same millisecond, and you want to make sure they end up in the same place in your dictionary, you'll need to truncate or round the DateTime value. I prefer to do this by constructing a new DateTime value using the properties from the old, though some calculation using Ticks is potentially faster:

public DateTime RoundToMillisecond(DateTime original)
{
return new DateTime(original.Year, original.Month, original.Day, original.Hour, original.Minute, original.Second, original.Millisecond);
}

How to convert the time in millisecs to DateTime?

You might try something like:

static DateTime GetDateTimeFromMillisecondNumber(int millisecCount)
{
return DateTime.Now.AddMilliseconds(millisecCount - Environment.TickCount);
}

But be aware that if the system was rebootet since the millisecCount was obtained, this returns rubbish. The same if this is run on a different machine from the one that you obtained the count from.

Also, this is only correct "modulo 49.7 days", i.e. the correct date and time might be an integral multiple of 49.7 days greater or less than what my method returns.

How to convert time string in milliseconds to DateTime object in c#?

Milliseconds is a duration, not a time. You can convert it to a TimeSpan easily:

string ms = "229935440730121";
TimeSpan ts = TimeSpan.FromMilliseconds(double.Parse(ms));

To convert to a DateTime you need to know the reference point from which the span was measured, then just add the TimeSpan to that date:

DateTime dt = DateTime.MinValue;  // for example only
dt += ts; // add the timespan to the date

In your example, that number of milliseconds represents over 7,280 years, so it's not clear what the reference point should be.



Related Topics



Leave a reply



Submit