When converting the current date and time, got from DateTime.Now, TimeZoneInfo.ConvertTimeToUtc(DateTime.Now) converts it correctly.
However, if a DateTime object is created, it does not convert it, it leaves it the same:
// get the local time as dd/mm/yyyy hh:mm:ss tt
DateTime dateTime = new DateTime(2020, 2, 5, 11, 59, 53, 0, DateTimeKind.Local); // tried with and without DateTimeKind specified
Console.WriteLine(dateTime.ToString());
// convert it to UTC
DateTime UTCdateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime);
Console.WriteLine(UTCdateTime.ToString());
This prints:
2/5/2020 11:59:53 AM
2/5/2020 11:59:53 AM
The second timestamp should be UTC.
Why is this?
Note: the values plugged in above I was using to test. I need to get them from dateTimePicker controls.
Related
Assume there's a table with a datetime column represented as ISO string. The datetime data is initially represented as NodaTime ZonedDateTime format in EST. and then it's converted to string format as 2021-02-10T02:07:07.000 -05 For example:
// datetime data represented as ZonedDateTime
var dateTimeUtc = new DateTime(2021, 2, 10, ,7, 7, 7, DateTimeKind.Utc);
var instant = Instant.FromDateTimeUtc(dateTimeUtc);
var zonedDateTime = instant.InZoneNewYork();
// convert datetime value to ISO string
var IsoString = zonedDateTime.Value.ToString("yyyy-MM-ddTHH:mm:ss;fff +o<HH>", CultureInfo.InvariantCulture);
Now I need to retrieve this value from table and represent it in ZonedDateTime EST format.
If I want to get this value from the this table, I can do something like:
var parsePattern = ZonedDateTimePattern.CreateWithInvariantCulture("yyyy-MM-ddTHH:mm:ss;fff +O<HH>", DateTimeZoneProviders.Tzdb);
However the returned string is represented as UTC time 2021-02-10T02:07:07 UTC (+00) which is not accurate (the actual saved value is EST).
Is there a way I can read ISO string with a specified time zone using NodaTime?
Is there a way I can read ISO string with a specified time zone using
NodaTime?
Changing format of ZonedDateTime Iso string representation, and preserving that format for both conversion operations should help.
Change "yyyy-MM-ddTHH:mm:ss;fff +O<HH>" to ZonedDateTimePattern.GeneralFormatOnlyIso.PatternText like:
var dateTimeUtc = new DateTime(2021, 2, 10, 7, 7, 7, DateTimeKind.Utc);
var instant = Instant.FromDateTimeUtc(dateTimeUtc);
DateTimeZone ny = DateTimeZoneProviders.Tzdb["America/New_York"];
var zonedDateTime = instant.InZone(ny);
// convert datetime value to ISO string
var IsoString = zonedDateTime.ToString(ZonedDateTimePattern.GeneralFormatOnlyIso.PatternText,
CultureInfo.InvariantCulture);
//parse iso string, like "2021-02-10T02:07:07 America/New_York (-05)"
var parsePattern = ZonedDateTimePattern.CreateWithInvariantCulture(
ZonedDateTimePattern.GeneralFormatOnlyIso.PatternText,
DateTimeZoneProviders.Tzdb);
var date2 = parsePattern.Parse("2021-02-10T02:07:07 America/New_York (-05)").Value;
I have used like this,
DateTime dueDate;
DateTime.TryParse(dataRead["Date required"].ToString(), out dueDate);
list.add(new list { DueDate = dueDate.ToShortDateString() });
I have also tried like this,
DateTime dueDate;
DateTime.TryParse(dataRead["Date required"].ToString("dd/MM/yyyy"), out dueDate);
list.add(new list { DueDate = dueDate.ToShortDateString() });
but it is not reflected. I also changed in access database format as Short date, but that is also not gave correct answer.
You can use dueDate.Date
DateTime.Date Property
Gets the date component of this instance.
Example
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
Results
// The example displays output like the following output:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
//
You can play with a demo here
Try this
DateTime dueDateTime = Convert.ToDateTime(row["Date required"].ToString());
DateTime dueDate= dueDateTime.Date;
First Convert it into Stting and then to DateTime
and then get Date from DateTime
I'm having strange behavior of DateTime in c#.
I'm trying to initialize a datepicker in January. So I make a new date:
DateTime MyDate = new DateTime(2017, 0, 15);
But I get this exception:
System.ArgumentOutOfRangeException: Year, Month, and Day parameters describe an un-representable DateTime.
If I use (2017, 1, 15) it works but the time dialog, initialized with:
DatePickerDialog dialog = new DatePickerDialog(
Activity,
this,
MyDate.Year,
MyDate.Month,
MyDate.Day);
Goes on February.
Well I tried to "cheat" and did:
DateTime MyDate = new DateTime(2017, 1, 15);
DateTime = DateTime.AddMonths(-1);
No error, but the date picker goes on February.
The only way to have January is:
DateTime MyDate = new DateTime(2017, 12, 15);
What am I doing wrong?
DateTime.Month is a value between 1 and 12, which aligns with what most people think a month 'number' is.
Per the android docs, theDatePickerDialog constructor you are calling accepts a zero-based month. It accepts values in the range 0-11, so you need to subtract one from the DateTime.Month.
DatePickerDialog dialog = new DatePickerDialog(Activity,
this, MyDate.Year, MyDate.Month - 1, MyDate.Day);
The issue is how the DateTime object treats month values (1-12) and how the DatePickerDialog treats month values (0-11).
DateTime constructor:
strange behavior of DateTime
DateTime MyDate = new DateTime(2017, 0, 15);
If we take a look at the DateTime constructor, it is clearly stated that the month value should be 1 through 12, which is not valid in your case and hence the exception. We can correct it as below:
DateTime MyDate = new DateTime(2017, 1, 15);
DatePickerDialog constructor:
Exception (or strange behavior) will arise when we try - new DatePickerDialog in combination with the month value of DateTime as the constructor of DatePickerDialog is expecting the month values from 0-11.
It is stated that int: the initially selected month (0-11 for compatibility with MONTH)
The approach which can then be followed is to give the correct index for month to DatePickerDialog constructor as below:
DateTime MyDate = new DateTime(2017, 1, 15);
DatePickerDialog dialog = new DatePickerDialog(Activity,
this,
MyDate.Year,
MyDate.Month - 1,
MyDate.Day);
I have to parse DateTime objects from strings with the yyyyMMddhhmmss format.
If I run this code, it works fine:
DateTime y = new DateTime(2013, 07, 22, 15, 35, 23);
string x = y.ToString("yyyyMMddhhmmss", CultureInfo.InvariantCulture);
But if I run this code - seemingly the inverse operation - I get an exception:
string x = "20130722153523";
DateTime y = DateTime.ParseExact(x, "yyyyMMddhhmmss", CultureInfo.InvariantCulture);
The exception is:
System.FormatException: String was not recognized as a valid DateTime.
I'm stumped as to what's wrong here. What am I doing wrong?
Note: Don't worry about timezones. I can deal with getting the correct timezone later.
The problem is that the date-time format you specified uses hh for a 12-hour time format, but the input string has 15 in that area. It can't parse this because 15 is outside the expected range.
Try using HH for a 24-hour time format instead:
string x = "20130722153523";
DateTime y = DateTime.ParseExact(x, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
Further Reading:
Custom Date and Time Format Strings
If I have a DateTime instance which represents a valid UTC time, and an offset that converts that DateTime to the time zone where it applies, how do I construct a DateTimeOffset instance to represent this?
var utcDateTime = new DateTime(2011, 02, 29, 12, 43, 0, /*DateTimeKind.Utc*/);
var localOffset = TimeSpan.FromHours(2.0);
var dto = ...
// Here the properties should be as follows;
// dto.UtcDateTime = 2011-02-29 12:43:00
// dto.LocalDateTime = 2011-02-29 14:43:00
Perhaps I'm not understanding the DateTimeOffset structure correctly, but I'm unable to get the expected output.
Thanks in advance
Looks like you want:
var utcDateTime = new DateTime(2012, 02, 29, 12, 43, 0, DateTimeKind.Utc);
var dto = new DateTimeOffset(utcDateTime).ToOffset(TimeSpan.FromHours(2));
Note that I changed the year from 2011 (which is not a leap year and does not have 29 days in February) to 2012.
Test:
Console.WriteLine("Utc = {0}, Original = {1}", dto.UtcDateTime, dto.DateTime);
Output:
Utc = 2/29/2012 12:43:00 PM, Original = 2/29/2012 2:43:00 PM
Do note that you probably don't want the LocalDateTime property, which may represent the instant in time as of the local system's timezone.