How do I format BetsAPI date time to local date time - c#

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;

Related

convert to DateTime long int |.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);

Unity c# convert date-string to datetime object in desired date time format

I need to convert a list of strings that contain date information (e.g. "2018-12-16") into DateTime format so that I can sort them all by date...and then convert back to string... I'm struggling to make this work. Testing on one of the strings:
string dateTimeString = "2018-12-16";
System.DateTime dateTime = System.DateTime.Parse(dateTimeString);
print(dateTime.ToString());
which prints "12/15/2018 12:00:00 AM" - but I don't want the time, and the format is different.. so I tried this (specifying the date format):
string dateTimeString = "2018-12-16";
System.DateTime.ParseExact(dateTimeString, new string[] { "yyyy-MM-dd" }, new System.Globalization.CultureInfo("en-AU"), System.Globalization.DateTimeStyles.None);
print(dateTime.ToString());
this has the same result.
I've tried making the format conversion going the other way:
string dateTimeString = System.String.Format("{0:yyyy-MM}", dateTime);
print(dateTimeString);
this also has the same result!
Can someone please help me, or point me in the direction of some decent documentation / examples?
Thanks!
Try this:
string dateTimeString = "2018-12-16";
System.DateTime dateTime = System.DateTime.Parse(dateTimeString);
print(dateTime.ToString("yyyy-MM-dd"));
More info can be found here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime?view=netframework-4.7.2#datetime-values-and-their-string-representations

Get only the date from a datetime in c#

I'm trying to pass a date in a viewbag like this
DateTime? DueDate = this.dbContext.OBProcessMonitors.Where(o => o.EmployeeID
== GlobalVariables.EmployeeID).First().DueDate;
ViewBag.DueDate = DueDate;
WHen i call the viewbag in razor #viewbag.duedate i get the date and time. How can I get just the date out of it?
Use 'DueDate.Value.Date' to get just the date of a datetime.
I assume that you need a date string, not a date object. So do following
To get short date string: like "6/15/2009" in en-US
Value.ToShortDateString()
Or to get long date string: like "Monday, June 15, 2009"
Value.ToLongDateString()
Or using custom format
Value.ToString("d")
Check other formats here: https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
You can convert DateTime to string and format it however you want to:
ViewBag.TheDate = yourobject.DueDate.ToString("dd/MM/yyyy") //or,
ViewBag.TheDate = yourobject.DueDate.ToString("MM/dd/yyyy") //or,
ViewBag.TheDate = yourobject.DueDate.ToString("yyyy/MM/dd") //etc...

c# string date format from d.M.yyyy to yyyy-MM-dd

Please, how I can convert date string from my csv import to grid and mysql database?
I import date in dd.MM.yyyy format and I must format yyyy-MM-dd for mysql database.
my code for datagrid rows is
dataGridView1.Rows.Add(counter, "1", lineIn[0], ControlPrice, lineIn[3]);
lineIn[0] is date string...
Thank you
Its best to use DateTime.ParseExact
Based on your example, you need to cast to string before passing to the function.
You should also consider what happens if an invalid date is in the CSV, how do you want to handle that? Or do you always expect a date and want to halt processing?
Here's a LinqPad example I used to test:
object date = "12.2.2014";
var result = DateTime.ParseExact(date.ToString(), "d.M.yyyy", null).ToString("yyyy-MM-dd");
Console.WriteLine(result);
See this SO post for more info: DateTime Conversion and Parsing DateTime.Now.ToString(“MM/dd/yyyy hh:mm:ss.fff”)
Just in case your record might come as blank, it will give error while parsing to datetime, so I have added a condition to check for blank and then converted it to the required format.
If(lineIn[0].ToString() != "")
{
dataGridView1.Rows.Add(counter, "1", DateTime.ParseExact(lineIn[0].ToString(), "dd.MM.yyyy", null).ToString("yyyy-MM-dd"), ControlPrice, lineIn[3]);
}
else
{
dataGridView1.Rows.Add(counter, "1", "", ControlPrice, lineIn[3]);
}
This should work for you:
var parsedDate = DateTime.ParseExact(lineIn[0], "dd.MM.yyyy", null);
string dateStringInAnotherFormat = parsedDate.ToString("yyyy-MM-dd");

Conversion of String to DateTime in based on system date formats in c#

I read date time in the following format from csv file '17-07-12 16:39:44 PM' (dd-MMM-yy hh:mm:ss tt ) and trying to convert that string to DateTime using DateTime.TryParseExact() API. However I receive input string in not valid date time error message. My system Date formats is different from the format I am trying to convert. How to convert the string to DateTime irrespective of what ever the system date time format is.
if (DateTimeOffset.TryParseExact("17-07-12 16:39:44 PM", "dd-MMM-yy hh:mm:ss tt"
, CultureInfo.InvariantCulture
, DateTimeStyles.None, out parsedDate) == false)
{
throw new ArgumentException("dateToPare", parsedDate);
}
I saw some similar post in this forum, but none of them gave solution to my problem. Please let me know if you need more description
Thanks,
Firstly convert string to datetime, then convert datetime to universal time before adding it to db.
public static DateTime AsUtc(this DateTime value)
{
if (value.Kind == DateTimeKind.Unspecified)
{
return new DateTime(value.Ticks, DateTimeKind.Utc);
}
return value.ToUniversalTime();
}

Categories