How to convert date to ms (UTC) and vice-versa? - c#

I'm making a C# windows form application. There is a DateTimePicker tool on the application and the tool's format is MM/dd/yyyy hh:mm:ss tt. For example, the date looks like this on the application: 07/28/2021 09:10:50 AM
I want to convert that date to milliseconds depending on UTC, and send that milliseconds to the server. I also need to do the opposite. When I receive the milliseconds from the server (UTC), I need to convert it to the format of the DateTimePicker (MM/dd/yyyy hh:mm:ss tt). I don't need to convert the date to the local time when I receive the milliseconds from the server. Everything will depend on UTC. How can I do that?

Use ParseExact to parse a specific format
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parseexact?redirectedfrom=MSDN&view=net-5.0#overloads
Be aware that depending on the tool/version using to accept the datetime, it may assume local-time/UTC so you may want to check the DateTime.Kind property.
https://github.com/dotnet/aspnetcore/issues/11584
If you are having trouble leverage DateTimeOffset to accept the value from the server and then pull out the raw DateTime from there using:
DateTimeOffset.DateTime

Related

Convert in UTC time format in c#

I need to set expire date of product as present date till midnight as shown below in UTC format.
"2021-05-28T23:59:59Z"
How i can write this in C#.
Use DateTime.UtcNow.Date
DateTime.UtcNow.Date.AddDays(1).AddSeconds(-1).ToString("o")
The output of above is
2021-05-28T23:59:59.0000000Z

C# change date between different formats

I want to know if there's a way to change the date format depending on the users local date format setting. The date I will store in the DB it's YYYY-MM-DD but the users have different formats, like DD-MM-YYYY or DD.MM.YYYY. What is a clean, elegant way to ensure that my application always retrives the date in local date format, and SQL server always receives the date in YYYY-MM-DD to be stored.
in the DB it's YYYY-MM-DD
No. If you do it right the storage in the Db does not have a format. It is stored, for example, as a number.
What is a clean, elegant way to ensure that my application always retrives the date in local date format
Your application receives it as a binary value too. You have to think about format every time it becomes a string.
in local date format
For that you could rely on the machine configuration: datevalue.ToString().
But usually you want to take control: datevalue.ToString(specificCultureInfo)
You should always save the date in a datetime object. To parse a date you can use DateTime.TryParseExcact when you know the date format and TryParse when you want to use your users prefrence. However it whould be nicer to use dedecated date controls to get a date from the user.
A DateTime object doesn't have a format. You're probably talking about the string representation of the date.
You can return the date as a string using the user's respective culture settings by calling DateTime.ToShortDateString()
DateTime date = new DateTime(2012, 01, 01);
string dateStringInCurrentCultureFormat = date.ToShortDateString();

Change date to Universal Time Format changes my date wrongly I can't get error

I am trying to simply change the date format from the datatable to universal time format but it formats it wrongly as if I have date for August 7 it changed it to August 8 after formatting it to universal date time. My code for formatting date is,
DateVar[runs] = DateTime.Parse(Convert.ToString(output.Tables[0].Rows[runs][0])).ToUniversalTime().ToString();
Don't get in to code its correct and its a part of loop so "run" is loop and output is data set having one table I have first data in table is "Sunday, August 07, 2011 10:52 PM" and it was converted to "8/8/2011 5:52:00 AM" after implementing universal time format.
Hopes for your suggestions
Universal time isn't a format - it's a time zone, effectively. It's not clear what you're trying to do, but converting a "local" DateTime to "universal" DateTime will usually change the time. If you don't want that to happen, don't call ToUniversalTime.
It's a pity that the .NET date/time API isn't as clear as it could be - the DateTime type itself has some horrible ambiguities about it. I'm trying to improve the situation with my Noda Time project, but you will need to understand what time zones are about etc.
Personally I would suggest not using simply DateTime.Parse or just calling ToString unless you're absolutely sure that the default format is what you want. I usually call DateTime.ParseExact and specify the expected format (and usually CultureInfo.InvariantCulture unless it's a user-entered string) - and likewise I provide a format string to the ToString call.
In your code you're simply converting a string to a string - what are you attempting to accomplish? If you're just trying to change the format (e.g. to dd/MM/yyyyTHH:mm:ss) then you don't need to call ToUniversalTime but you do need to provide the format string.
I suggest you split your code out into several statements to help you debug this (and for general code clarity):
Fetch the string from the DataTable, if you really need to (if it's already a DateTime, there's no point in converting it to a string and then back again)
Parse the string (again, assuming you need to)
Perform any conversions you need to
Format the DateTime with an explicit format string
Now if any single operation is causing a problem, you can isolate it more easily.
If I run ToUniversalTime() from Greenwich it will give same time but if i do it while I live some where else it will get an offset date time object of + or - hours depending on position.

DateTime Format Problem To Get Same Format

I have some questions about using datetime format.
In one part of project,client pc send their datetime to server.
we need to get those datetime in same format like dd/MM/yyyy.
However,client pc use variety of date format,so,they send
variety of datetime format like this.for eg,
dd-MM-yyyy,dd/MM/yyyy,MM-dd-yyyy,MM/dd/yyyy
How can I solve this problem?
The absolute best way is to not treat date values as strings. They should to the greatest extent possible be treated as DateTime values. When doing that, all issues related to formatting disappears. If you have a client where the user enters date format in their local style, convert it to a DateTime directly after input, and then send the DateTime value into the system.
If you still need to exchange date information in string format, always stick to a standardized format (such as ISO 8601).

Convert facebook date time to C# date time

Hi i have notice there two date times facebook gives, one is a date time from Unix Epoch and other is RFC 3339 Datetime (if i am not wrong). I wanted to know what is the best way to convert between each other. I have tried DateTime.TryParse() method but some times it dosent return the correct parsed date. Like it gives 21-Dec-2010 7:21:56 AM for 2010-12-21T01:51:56+0000. But it seems it doesn't parse time correctly. So plese tell how to change between epoch times and above datetime in C# datetime.
DateTime.Parse is converting a UTC date to your local timezone.
To retrieve the original UTC date, call ToUniversalTime().

Categories

Resources