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
Related
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
I am storing date and time separately from Angular to C#. While storing in database, I combine start date and start time field in C# and store it in dAtabase UTC like this: "2020-02-28T22:30:30Z".
While returning from c#, i create a new DateTime with start date and start time and return as one variable. However, if the date is 28/02/2020 and time is 4.00 am, with timezoneoffset of 5.30 India, the date gets rendered to 29/02/2020 4.00 am.
Is it possible to get date and time in Angular and render it separately as string etc.
Thanks
In JavaScript Date is a timestamp, counts the number of miliseconds since January 1, 1970 00:00 UTC. So you might be having a problem with timezones. Check this answer I gave a couple of weeks ago, it may guide you. I also add a function to solve it and some references there, Subtract day in conversion between Moment and LocalDate.
angular set timezone based on user browser timezone so you can change datetimes to another timezone. please read it How to convert Date in angular to another time zone
I have coded a MVC 5 internet application and have a question in regards to international dates.
I am wishing to add a create date field to each of my objects when my objects are saved into a SQL database. As this application will be used internationally, what format should I use for the dates and do I need to use a specific UTC?
Thanks in advance
The most common .NET type is DateTime. A DateTime stores a date and optionally a time. It has a Kind property which determines if the value is one of three: local, UTC, or unspecified.
Another type is called DateTimeOffset. A DateTimeOffset stores a date and time relative to UTC. Additionally, it stores an offset from UTC as TimeSpan.
Console.WriteLine (DateTime.Now); // 1/28/2014 2:53:50 PM
Console.WriteLine (DateTimeOffset.Now); // 1/28/2014 2:53:50 PM -08:00
More Information : http://afana.me/post/aspnet-mvc-internationalization-date-time.aspx
I am reading a date from an xml file and parsing it to a my desired format. It i adding a day to the date and i cant seem to figure out why.
input : 2014-02-12T15:21:19-08:00
output : 13 Feb 2014 01:21
Here is my code to parse date:
string date = DateTime.Parse(row["CountDate"].ToString()).ToString("dd MMM yyyy HH:mm");
Any help will be greatly appreciated.
The reason is that the timezone information is being used to adjust the time to your local time zone.
If you remove the "-08:00" suffix, you'll find that the time won't be adjusted. However, you need to know whether the timezone information is important before ignoring it!
Well it looks like what you have is a UTC date/time with an 8 hour offset, when you parse the date what you have is an instance of the local time (Parse will take into account the offset).
If you are only interested in the UTC date/time then you can parse just that particular information
DateTime.ParseExact("yyyy-MM-ddTHH:mm:ss", row["CountDate"],
CultureInfo.InvariantCulture);
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().