How to convert string to local Date Time? - c#

I am trying to convert a string of this format:
MM/dd/yyyy HH:mm
The input is from a US database, so, i.e.:
09/20/2010 14:30
I know that my string is always US time but when I display it, I need to translate that into the local time, so that string should be turned into:
09/20/2010 19:30 (for UK for instance)
I tried a few things but nothing seems to give me the correct solution when I run on a US machine vs a UK or Ge machine
I tried:
CompletedDttm = DateTime.ParseExact(value, "MM/dd/yyyy HH:mm", CultureInfo.CurrentCulture);
CompletedDttm = DateTime.ParseExact(value, "MM/dd/yyyy HH:mm", new CultureInfo("en-US"));
They all work locally (US machine) but they don't convert the time to local time on a European machine.
Thanks
Tony

Try this - it converts local time (input in US format) to GMT and then prints in GB/DE format.
var zones = TimeZoneInfo.GetSystemTimeZones(); // retrieve timezone info
string value = "09/20/2010 14:30";
DateTime CompletedDttm = DateTime.ParseExact(value, "MM/dd/yyyy HH:mm",
new CultureInfo("en-US"));
DateTime FinalDttm = TimeZoneInfo.ConvertTime(CompletedDttm,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"));
string output = FinalDttm.ToString(new CultureInfo("en-GB"));
FinalDttm = TimeZoneInfo.ConvertTime(CompletedDttm, TimeZoneInfo.Local,
TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"));
output = FinalDttm.ToString(new CultureInfo("de-DE"));
Output is, in turn:
20/09/2010 19:30:00
20.09.2010 20:30:00

UPDATE: You have to know the timezone of the data (not just that it is "US") as well as the interpreting machine if you want to reliably convert it to anything else. You're not only looking at hours offset, but DST also which varies by location (not all locales abide by it). Eastern is either -4 or -5 depending on the time of year. And if the date is old enough you run into the issue that "summer" dates were changed recently.
Your best course is to ALWAYS store timestamps in UTC. Aside from that, you can just make guesses about the offset.
You should be working with UTC times (the new, slightly different, version of GMT) if you want to be converting to other time zones.
DateTime dt = new DateTime(DateTime.Parse('2010-10-06 19:40').Ticks, DateTimeKind.Local);
dt.AddHours(5);
dt.ToLocalTime();
You could also make use of TimeZoneInfo which will have DST information also.

Unless you specify otherwise, the parse will assume you mean to parse the string into your current timezone. US culture just means the expected format of the string, and has nothing to do with the timezone (for example, in the US it could be EST or it could be PST).
Your string contains no timezone information, so naturally you're going to get your value in whatever the local timezone is. You can either:
Add the timezone info
Change the timezone afterwards

I think it's a display problem, but need more info to be sure. Try displaying the dates in yyyy-MM-dd format in both cases to check if the problem is on parse or display. You can create a custom format info object if you know exactly what you want to accept or display:
public static DateTimeFormatInfo GetISOFormatInfo()
{
DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
dtFormat.DateSeparator = "-";
dtFormat.TimeSeparator = ":";
dtFormat.ShortDatePattern = "yyyy-MM-dd";
dtFormat.ShortTimePattern = "HH:mm:ss";
return dtFormat;
}

Using a Date without TimeZone information, you will not be able to know the UK time / Canada time etc... since you do not know who (which part of the world) instered that time. Since you specifically said that the time is US time, you can add the time difference for the different parts of the world to display the local time.

You could use string.Split. first with the '/' separator on the whole string. You will get "09" "20" and "2010 14:30" then apply the split 2 more times with ' ' and ':'

Related

How can I print DateTime in UTC format?

I'm simply trying to print a DateTime in its UTC equivalent time format. What am I doing wrong?
var utcEpoch = DateTime.Parse("1970-01-01", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal); //This specifies the time I provided is in UTC
Console.WriteLine(utcEpoch.ToString("yyyy-MM-dd HH:mm:ss zzz")); //This properly shows my UTC offset of -6, so it's not wrong
Console.WriteLine(utcEpoch.ToString("u")); //This just flat out seems wrong because it doesn't specify a timezone or offset in its output
> 1969-12-31 18:00:00 -06:00
> 1969-12-31 18:00:00Z
I expected to see 1970-01-01 00:00:00Z for the last one.
From The Universal Sortable ("u") Format Specifier :
Although the result string should express a time as Coordinated
Universal Time (UTC), no conversion of the original DateTime value is
performed during the formatting operation. Therefore, you must convert
a DateTime value to UTC by calling the DateTime.ToUniversalTime method
before formatting it. In contrast, DateTimeOffset values perform this
conversion automatically; there is no need to call the
DateTimeOffset.ToUniversalTime method before the formatting operation.
Your utcEpoch.Kind is not UTC, it is Local. DateTime's are triciker than you might think. You are expecting that it will return UTC as Kind property but it is not. It returns Local.
This situation has been discussed on Phil Haack blog post as well and Matt Johnson has a quite nice comment about this;
AssumeLocal and AssumeUniversal are both related to how the input
string is interpreted. By themselves, neither will change the output
kind.
The default output kind is Local. To get it to be Utc, you can use the
AdjustToUniversal style.
The DateTimeStyles enum is flags-based, so you can combine these in
some ways that make sense. To achieve what you originally set out to
do (parse the input as UTC and output it as UTC), then you would use:
DateTime utcDate = DateTime.Parse("10/01/2006 19:30", culture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);
As others pointed pointed out, a separate call to ToUniversalTime()
would also work, but this is technically more correct.
You can see it on referance source as well;
case 'u': // Universal time in sortable format.
if (offset != NullOffset)
{
// Convert to UTC invariants mean this will be in range
dateTime = dateTime - offset;
}
else if (dateTime.Kind == DateTimeKind.Local)
{
InvalidFormatForLocal(format, dateTime);
}
I think you are missunderstanding what the API is doing.
First thing to note is that both DateTimeStyles.AssumeUniversal and DateTimeStyles.AssumeLocalwill still return a DateTime where Kind = Local
> DateTime.Parse("1970-01-01 00:00:00", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).Kind
=> Local
> DateTime.Parse("1970-01-01 00:00:00", CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal).Kind
=> Local
So no matter what we will get a local date. That means that the API most likely is there to make it possible to get a local time from a UTC date. Let's try if that's correct.
I'm in Sweden so we are UTC + 1 during standard time. So if I use DateTimeStyles.AssumeUniversal and put in todays date I should get a local date being 01:00 today.
Running this in C# Interactive:
> DateTime.Parse("2018-03-03", .CultureInfo.InvariantCulture, .DateTimeStyles.AssumeUniversal)
=> [2018-03-03 01:00:00]
Meaning C# assumed that the string I inputed was in UTC and I wanted it in local so it "fixed" it for me.
Doing the same with AssumeLocal.
DateTime.Parse("2018-03-03", .CultureInfo.InvariantCulture, .DateTimeStyles.AssumeLocal)
=> [2018-03-03 00:00:00]
As expected we now treated the input as a local string and got the same value.
To get the date as UTC you can specify the kind
DateTime.SpecifyKind(DateTime.Parse("2018-03-03", CultureInfo.InvariantCulture), DateTimeKind.Utc).ToString("o")
=> "2018-03-03T00:00:00.0000000Z"

Parse date time c# with correct timezone and kind

I have a datetime in database which I read using SqlDataReader and then cast it to (DateTime). After the cast its Kind property is DateTimeKind.Unspecified.
Then I have another string which I read from some other source. Its format is like this 2016-01-20T22:20:29.055Z. I do DateTime.Parse("2016-01-20T22:20:29.055Z") and its Kind property is DateTimeKind.Local.
How do I properly parse the both date times for comparison? Do I need to use DateTimeOffsets? How should I parse them?
Thanks
Because SQLReader cannot reasonably infer a DateTimeKind, it leaves it as unspecified. You'll want to use DateTime.SpecifyKind to change the DateTimeKind on your output from the SQLReader to the appropriate value. This works ok if you are only dealing with UTC and one consistent local time zone; otherwise, you really should be using DateTimeOffset in both your code and the SQL Database.
The string "2016-01-20T22:20:29.055Z" is ISO 8601 compliant and is a UTC date; however, DateTime.Parse with only 1 argument can end up performing a conversion to local time. Per the documentation:
Generally, the Parse method returns a DateTime object whose Kind
property is DateTimeKind.Unspecified. However, the Parse method may
also perform time zone conversion and set the value of the Kind
property differently, depending on the values of the s and styles
parameters:
If s contains time zone information, the date and time is converted
to the time in the local time zone and the Kind is DateTimeKind.Local.
If s contains time zone information, and styles includes the
AdjustToUniversalflag, the date and time is converted to Coordinated
Universal Time (UTC) and the Kind is DateTimeKind.Utc.
If s contains the Z or GMT time zone designator, and styles includes
the RoundtripKind flag, the date and time are interpreted as UTC and
the Kind is DateTimeKind.Utc.
Also see UTC gotchas in .NET and SQL Server in Derek Fowler's blog for additional coverage on the topic.
In your second example, 2016-01-20T22:20:29.055Z has timezone information provided with it; the 'Z' at the end indicates that the timestamp is intended for Coordinated Universal Time (UTC). However, DateTime.Parse() will default its conversion using DateTimeKind.Local unless a specific timezone is specified. You can use DateTime.ParseExact to be more specific.
As to why the datetime values in your database are coming out as Unspecified, that's likely because they contain no timezone indication at all. Check to see if your database values specify timezone information, either by using 'Z' at the end or specifying an exact timezone, such as 2016-01-20T22:20:29.055-07:00 (UTC-7).
You can use something like this:
string format = "ddd dd MMM h:mm tt yyyy";
DateTime dateTime = DateTime.ParseExact(dateString, format,
CultureInfo.InvariantCulture);
In format variable, you can put the format you want, and pass it to ParseExact function.
Hope it helps.
You are missing the datetime context (offset) in your database. You should persist it either in a datetimeoffset column or in a datetime column but persisting utc datetimes.
And always better compare two utc datetimes.
I coded a quick C# console app that I pasted in below. This converts a UTC date and time to a string (format similar to the ISO 8601 format described in another post with some extra digits of precision), writes it to a file, reads it from the file (as a string) and then converts it back to a UTC date and time.
It then compares the two UTC Date Time objects, which are both of UTC kind, and they match.
class Program
{
// "2016-01-20T22:20:29.055Z" is ISO 8601 compliant and is a UTC date
const string dtf = "yyyy-MM-ddTHH:mm:ss.fffffffZ";
static void Main(string[] args)
{
string file = #"c:\temp\file.txt";
DateTime dt = DateTime.UtcNow;
using (var sw = new System.IO.StreamWriter(file))
{
sw.WriteLine(dt.ToString(dtf, System.Globalization.CultureInfo.InvariantCulture));
}
DateTime dtin;
using (var sr = new System.IO.StreamReader(file))
{
dtin = DateTime.ParseExact(sr.ReadLine(), dtf, System.Globalization.CultureInfo.InvariantCulture);
}
Console.WriteLine(dt.ToString(dtf) + "\r\n" + dtin.ToString(dtf) + "\r\nEquality:" + (dt == dtin));
Console.ReadLine();
}
}

How to convert UTC+0 Date to PST Date?

I have this UTC+0 Date :
2011-11-28T07:21:41.000Z
and I'd like, on C#, convert it to a PST Date. How can I do it? Tried with :
object.Data.ToLocalTime()
but I can't get the correct value (which should be 2011-11-27)
EDIT
Also tried (after suggesion on another topic) this :
DateTime convertedDate = DateTime.SpecifyKind(
DateTime.Parse(object.Data.ToShortDateString()),
DateTimeKind.Utc);
DateTime dt = convertedDate.ToLocalTime();
string dataVideo = dt.ToShortDateString();
but the date still 28/11/2011, not 27/11/2011
I've changed my clock to use UTC-08:00 Pacific Time.
DateTime timestamp = DateTime.Parse("2011-11-28T07:21:41.000Z");
Console.WriteLine("UTC: " + timestamp.ToUniversalTime());
Console.WriteLine("PST: " + timestamp.ToLocalTime());
Output:
UTC: 28/11/2011 7:21:41
PST: 27/11/2011 23:21:41
Example with TimeZoneInfo
DateTime timestamp = DateTime.Parse("2011-11-28T07:21:41.000Z");
Console.WriteLine("UTC: " + timestamp.ToUniversalTime());
Console.WriteLine("GMT+1: " + timestamp.ToLocalTime());
Console.WriteLine("PST: " + TimeZoneInfo.ConvertTimeBySystemTimeZoneId(timestamp, "Pacific Standard Time"));
Output:
UTC: 28/11/2011 7:21:41
GMT+1: 28/11/2011 8:21:41
PST: 27/11/2011 23:21:41
For a little more color
2011-11-28T07:21:41.000Z
This is a ISO8601 Timestamp, the Z at the end stands for UTC. This represents a specific instance in time.
DateTime.Parse will return to you a local date time structure, there are three types of datetime kinds, UTC, Local, and Unspecified.
If you try displaying this, it will show you this instant in your computers current timezone (I'm eastern time so when I print it I get 11/28/2011 2:21:41 AM).
If I want to switch this DateTime Kind to UTC, I could do something like
DateTime.Parse("2011-11-28T07:21:41.000Z").ToUniversalTime()
Printing this now (since it's kind is now UTC) I get 11/28/2011 7:21:41 AM.
Note that although the time is printed differently both these date times are referring to the same instant in time.
To display this instant in a different timezone, the easiest way imo is the TimeZoneInfo class (though I'm not sure it's 100% accurate).
TimeZoneInfo.ConverTimeBySystemTimeZoneId(dateTime, "Pacific Standard Time").
Printing it now will yield your desired result 11/27/2011 11:21:41 PM
Note that this return DateTime's Kind property is now Unspecified, meaning you won't be able to transfer it back to UTC without more information. You no longer have a specific instant in time, rather you have a unspecified time..we know it's the same instant as the ones previously just in pacific time, but the computer no longer knows that. Keep that in mind if you want to store this time.

Converting UK times (both BST and GMT) represented as strings to UTC (in C#)

I have to use some dates and times from a legacy database. They are represented as strings. Dates are dd/MM/yy. Times are HH:mm.
I'd like to convert these to UTC as soon as I pull them from the database. I'm working on US systems, so need a common time.
The problem I'm facing is how to convert them to UTC DateTime values. I can do the parsing, etc. The real problem I have concerns the timezone.
I'm trying to use the following approach:
DateTime ukTime = // Parse the strings in a DateTime value.
TimeZoneInfo timeZoneInformation = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTimeOffset utcTime = new DateTimeOffset(ukTime, timeZoneInformation.BaseUtcOffset);
However, this gives incorrect values if the date is in the British Summer Time period.
I can use "GMT Daylight Time" on those dates, but that requires me to know when the switchover is. I'm sure there must be a less laborious way.
As I'm not using a machine with UK time settings I can't rely on local time.
Basically, I need something like:
// Works for both GMT (UTC+0) and BST (UTC+1) regardless of the regional settings of the system it runs on.
DateTime ParseUkTimeAsUtcTime(string date, string time)
{
...
}
I've scoured the posts, but couldn't find anything that addressed this directly. Surely this is also an issue with EST, EDT, etc?
Try using the GetUtcOffset() method on your TimeZoneInfo instance, which takes "adjustment rules" into consideration.
Using this should work basically the same as your original example, but you'll use that method instead of the BaseUtcOffset property.
DateTime ukTime = // Parse the strings in a DateTime value.
TimeZoneInfo timeZoneInformation = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTimeOffset utcTime = new DateTimeOffset(ukTime, timeZoneInformation.GetUtcOffset(ukTime));
How about:
DateTime.Parse(dateTimeString).ToUniversalTime();
Assuming that the database server stores its datetimes in the same timezone as your application server.

C#: How would I get the current time into a string?

How could I get the current h/m/s AM time into a string? And maybe also the date in numeric form (01/02/09) into another one?
I'd just like to point out something in these answers. In a date/time format string, '/' will be replaced with whatever the user's date separator is, and ':' will be replaced with whatever the user's time separator is. That is, if I've defined my date separator to be '.' (in the Regional and Language Options control panel applet, "intl.cpl"), and my time separator to be '?' (just pretend I'm crazy like that), then
DateTime.Now.ToString("MM/dd/yyyy h:mm tt")
would return
01.05.2009 6?01 PM
In most cases, this is what you want, because you want to respect the user's settings. If, however, you require the format be something specific (say, if it's going to parsed back out by somebody else down the wire), then you need to escape these special characters:
DateTime.Now.ToString("MM\\/dd\\/yyyy h\\:mm tt")
or
DateTime.Now.ToString(#"MM\/dd\/yyyy h\:mm tt")
which would now return
01/05/2009 6:01 PM
EDIT:
Then again, if you really want to respect the user's settings, you should use one of the standard date/time format strings, so that you respect not only the user's choices of separators, but also the general format of the date and/or time.
DateTime.Now.ToShortDateString()
DateTime.Now.ToString("d")
Both would return "1/5/2009" using standard US options, or "05/01/2009" using standard UK options, for instance.
DateTime.Now.ToLongDateString()
DateTime.Now.ToString("D")
Both would return "Monday, January 05, 2009" in US locale, or "05 January 2009" in UK.
DateTime.Now.ToShortTimeString()
DateTime.Now.ToString("t");
"6:01 PM" in US, "18:01" in UK.
DateTime.Now.ToLongTimeString()
DateTime.Now.ToString("T");
"6:01:04 PM" in US, "18:01:04" in UK.
DateTime.Now.ToString()
DateTime.Now.ToString("G");
"1/5/2009 6:01:04 PM" in US, "05/01/2009 18:01:04" in UK.
Many other options are available. See docs for standard date and time format strings and custom date and time format strings.
You can use format strings as well.
string time = DateTime.Now.ToString("hh:mm:ss"); // includes leading zeros
string date = DateTime.Now.ToString("dd/MM/yy"); // includes leading zeros
or some shortcuts if the format works for you
string time = DateTime.Now.ToShortTimeString();
string date = DateTime.Now.ToShortDateString();
Either should work.
Method to get system Date and time in a single string
public static string GetTimeDate()
{
string DateTime = System.DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss");
return DateTime;
}
sample OUTPUT :-16-03-2015 07:45:15
DateTime.Now.ToString("h:mm tt")
DateTime.Now.ToString("MM/dd/yyyy")
Here are some common format strings
Be careful when accessing DateTime.Now twice, as it's possible for the calls to straddle midnight and you'll get wacky results on rare occasions and be left scratching your head.
To be safe, you should assign DateTime.Now to a local variable first if you're going to use it more than once:
var now = DateTime.Now;
var time = now.ToString("hh:mm:ss tt");
var date = now.ToString("MM/dd/yy");
Note the use of lower case "hh" do display hours from 00-11 even in the afternoon, and "tt" to show AM/PM, as the question requested. If you want 24 hour clock 00-23, use "HH".
string t = DateTime.Now.ToString("h/m/s tt");
string t2 = DateTime.Now.ToString("hh:mm:ss tt");
string d = DateTime.Now.ToString("MM/dd/yy");
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Categories

Resources