convert to DateTime long int |.C# - c#

In my api response I have key such long int type.
{
"1640908800000": 123945000000,
"1648684800000": 97278000000,
"1656547200000": 82959000000,
"1664496000000": 90146000000,
"index": "TableIndex"
},
{
"1640908800000": 69702000000,
"1648684800000": 54719000000,
"1656547200000": 47074000000,
"1664496000000": 52051000000,
"index": "BookIndex"
}
1664496000000 key It must be DateTime. I didn't see such example. According my api and information from website it must be 9/29/2022
How I can parse it to normal DateTime Format?

This seems to be Unix timestamp in milliseconds, to convert it to DateTime you need to parse it as long and then process for example with DateTimeOffset.FromUnixTimeMilliseconds:
DateTime dt = DateTimeOffset.FromUnixTimeMilliseconds(long.Parse("1664496000000"))
.UtcDateTime;
Console.WriteLine(dt); // Prints "9/30/2022 12:00:00 AM"

That looks like Unix timestamp so this is how you can convert it to datetime:
DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);

Related

How do I format BetsAPI date time to local date time

I am trying to figure out what datetime format BetsAPI uses and how to convert it to local date time. I get the information through a JSON file.
Sample Code: updated_at is what I am trying to convert.
"schedule": {
**"updated_at": "1557235827",**
"sp": {
"main": [
{
"odds": "2.000"
},
{
"odds": "1.800"
}
]
}
}
Your updated_at property is just a classic timestamp.
If you parse 1557235827 in a conversion website such as this one, you will see the datetime in a more readable format : GMT: Tuesday 7 May 2019 13:30:27.
You can use DateTimeOffset to convert unix time to DateTime
var update_at = 1557235827;
var updateAtTime = DateTimeOffset.FromUnixTimeSeconds(update_at).DateTime;

How to convert a mysql datetime string to c# datetime string?

I get "10/1/15 12:00:00 AM" time string from mysql db. How to convert this to a System.DateTime by the ParseExact method?
You can use DateTime.ParseExact method with a custom date and time format.
I assume your 10 is as a day;
var dt = DateTime.ParseExact("10/1/15 12:00:00 AM", "dd/M/yy hh:mm:ss tt",
CultureInfo.InvariantCulture);
By the way, of course saving your DateTime as a string in your database is a bad idea. Change your column type to DATETIME data type if you can.
Read: Bad habits to kick : choosing the wrong data type

C# with mongodb DateTime Convert

I want to convert "ISODate(\"2014-11-13T18:43:33.868Z\")" to c# datetime ex 2014-11-13 18:43:33.
Value "ISODate(\"2014-11-13T18:43:33.868Z\")" take from MongoDB collection.
Please Help.
You can set DateTime in C# to UTC
var createDate = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc)
or
dateTime = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc)
Then insert type DateTime into mongodb
It worked in my case.
You can store your date as a BsonDateTime object when you pull it from the database, then convert it as follows:
DateTime dt = bdt.ToUniversalTime();
And you may find this question useful to learn more about how ToUniversalTime() works.
If I understand clearly, just because it writes ISODate in your string, that doesn't make it ISO 8601 format. The "O" or "o" standard format specifier complies ISO 8601 format and which is "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string for a DateTime. That doesn't match with your string format.
If your all strings has a stable format like this, you can use custom date and time formats with literal string delimiter like;
string s = "ISODate(\"2014-11-13T18:43:33.868Z\")";
string format = "'ISODate(\"'yyyy-MM-dd'T'HH:mm:ss.fff'Z\")'";
DateTime date;
if(DateTime.TryParseExact(s, format, CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
Console.WriteLine (date);
}
If you want to string representation of your DateTime with "2014-11-13 18:43:33" format, you can use DateTime.ToString() method like;
date.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

Convert DateTime to custom Date format

I have a DateTime object with value as 2011-08-11T01:03:29+00:00 which is returned from a database.
How can I convert this to mm/dd/yyyy format where as the end result type should be DateTime object only, not string?
A DateTime object is only the numerical representation of the date as 'ticks' from a constant start time (for example, January 1, 0000). It is not the string representation. To get a string representation, you do ToString() on the object.
To convert your DateTime to a string for your custom format, use
myDateTime.ToString("MM/dd/yyyy");
See http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx for complete details about DateTime.ToString() custom formats.
See http://msdn.microsoft.com/en-us/library/system.datetime.aspx:
"Internally, all DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight, January 1, 0001. The actual DateTime value is independent of the way in which that value appears when displayed in a user interface element or when written to a file."
Pass it as a string and parse it
format = "mm/dd/yyyy";
try {
result = DateTime.ParseExact(yourDate.ToString(format), format, provider);
}
catch (FormatException) {
Console.WriteLine("{0} is not in the correct format.", dateString);
}
MSDN has all answers
DateTime has functions called ToLongDateString() and ToShortDateString() (I believe those are correct function names) and the to short one does what you want.

how to convert date with 'T' to/from string in C#

I used following functions to convert DateTime from/into string:
DATE_OBJ.ToString(DATE_FORMAT);
DateTime.ParseExact(Date_string, DATE_FORMAT, null);
Now I've got to work with follow format 2012-03-20T14:18:25.000+04:00
Which format should I use to convert it correctly to string and generate string like that from DateTime object?
You can go from DateTime to that format with
DateTime dt = new DateTime();
dt.ToString("o");
and from that format to DateTime with
DateTimeOffset.Parse(dateString);
Here is some more info on DateTime format:
http://www.dotnetperls.com/datetime-format
You are better of using DateTimeOffSet like:
string str = " 2012-03-20T14:18:25.000+04:00";
DateTimeOffset dto = DateTimeOffset.Parse(str);
//Get the date object from the string.
DateTime dtObject = dto.DateTime;
//Convert the DateTimeOffSet to string.
string newVal = dto.ToString("o");
You cannot do this from DateTime, as DateTime holds no TimeZone info.
This is close: string.Format("{0:s}", dt) will give 2012-03-20T14:18:25.
See: http://www.csharp-examples.net/string-format-datetime/
You could extend this to: string.Format("{0:s}.{0:fff}", dt), which will give 2012-03-20T14:18:25.000
But you better have a look at DateTimeOffset: DateTime vs DateTimeOffset
(Not advisable, but to fake it and still use DateTime: string.Format("{0:s}.{0:fff}+04:00", dt))
If that is a string you receive then you can split the string by T and use only the first part which is the Date component of the whole string and parse that.
ex:
string dateTimeAsString = "2012-03-20T14:18:25.000+04:00";
string dateComponent = dateTimeAsString.Splic('T')[0];
DateTime date = DateTime.ParseExact(dateComponent, "yyyy-MM-dd",null);

Categories