c# parse date time with timezone - c#

How can we parse date time with time zone.
<TIMESTAMP_UTC>20180523160000</TIMESTAMP_UTC>
<TIMEZONE>UTC+8</TIMEZONE>
this should convert as in 2018-05-24 00:00:00.
I tried couple of things but could not succeed.
I tried the below command but it throws an error.
DateTime.ParseExact("20180523160000+08:00", "yyyyMMddHHmmssZhhmm", System.Globalization.CultureInfo.InvariantCulture)
Do you know how we can parse this with DateTime Parse methods.

You need to use DateTimeOffset.ParseExact
var date = DateTimeOffset.ParseExact("20180523160000+08:00", "yyyyMMddHHmmsszzz", CultureInfo.InvariantCulture);

Try using DateTimeOffset.Parse() instead of DateTime.Parse() as DateTimeOffset stores timezone info.
You can refer to the following MSDN link for more details:
https://msdn.microsoft.com/en-us/library/bb351654(v=vs.110).aspx

You could invert the sign of your timezone and then parse it with
var dateTime = DateTime.ParseExact(
"20180523160000UTC-8",
"yyyyMMddHHmmssUTCz",
CultureInfo.InvariantCulture);
But if this is a good approach is probably questionable.

Related

convert resulting string datetime to dateTime value

i am fetching datetime value through xml like:
string time = "20150605020247+0000"
I want to convert into datetime value. I tried with DateTime.Parse, ParseExact, Convert.ToDateTime. It's not working, it's returning the error:
string was not recognised as valid datetime
You should use DateTime.ParseExact like this
DateTime theTime = DateTime.ParseExact(time, "ddMMyyyyHHmmss+ffff,CultureInfo.InvariantCulture);
You should be able to use DateTime.ParseExact, since you know the exact format of the string. If we assume it's year, month, day, hour, minute, second, and offset, then you can do something like:
var result = DateTime.ParseExact("20150605020247+0000", "yyyyMMddHHmmsszzz",
CultureInfo.InvariantCulture);
You need to use exact format specifier in your ParseExact method.
DateTime.ParseExact("20150605020247+0000", "yyyyMMddHHmmsszzz", System.Globalization.CultureInfo.InvariantCulture);
Fiddle: https://dotnetfiddle.net/cQJ9hN
EDIT:
Please check the standard DateTime formats used in .NET world: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings plus check the source of your data how exactly are the string dates produced.
However most likely the '+####' part of your string is the local date UTC offset, not the fractions part of the time (as other answers suggest). So parsing the date by using the "yyyyMMddhhmmssffff" would produce wrong results.

How to Convert String Date in DateTime form in ASP.NET C#

I have a string Date which is coming from Database I need to parse or convert that String Date in DateTime form. Sharing the data and code as of now sharing the date which is coming from DB
String Date="7/19/2010 7:34:43 AM";
// I am parsing in DateTime form by below code
Date= DateTime.Parse(Date).ToString("yyyy-MM-dd HH:mm:ss.fff");
But I am getting the error while parsing with existing code as String was not recognized as a valid DateTime.
Can anyone please share some info how can I resolve this error so that I wont receive any exception
Note
Issue with my code is the date which is coming from Database is not a valid string type that's why I am getting the error string is not recognized as valid datetime
You should do, using DateTime.ParseExact
DateTime dt = DateTime.ParseExact("7/19/2010 7:34:43 AM",
"M/d/yyyy h:mm:ss tt",
CultureInfo.InvariantCulture);
DateTime.ParseExact(yourstring, "yyyyMMdd_HH:mm:ss.fff", new CultureInfo("en-US"));
Will solve your issue.
For reference follow :
Date Time Parse
Date Time Parse Exact
Either you can use
DateTime.ParseExact or DateTime.TryParseExact
This will allow you to specify specific formats.
I prefer the TryParseExact as they provide good coding style for the casing error.

Cant parse string to datetime

DateTime startDate = DateTime.ParseExact("2011-05-25 24:00:00", "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
for some reason parsing this string to a datetime does not want to work. I Tried several things already but it just does not want to work. Most of the internet examples do it like this too.
Does someone sees what is wrong?
thanks
"24:00:00" is not a valid time. Should probably be "00:00:00". If you meant the second that comes after 2011-05-25 23:59:59, that would be 2011-05-26 00:00:00.
For more information about valid value ranges for different format specifiers, check Custom Date and Time Format Strings at MSDN.

Help converting a string date to a DateTime

I'm using the Ajax control toolkit calendar extender on a textbox with a submit button. Simple.
The debugger shows that the text is properly being transferred to calling method, but this line of conversion code converts textbox text to 1/1/0001 12:00:00 AM. The text box date is this: 4/15/2011
DateTime txtMyDate = Convert.ToDateTime(txtDate.Text);
What am I doing wrong?
You should use the DateTime.Parse() method:
DateTime txtMyDate = DateTime.Parse(txtDate.Text);
As mentioned you can also use DateTime.ParseExact() using a similar syntax as shown:
DateTime txtMyDate = DateTime.ParseExact(txtDate.Text,
[string format],
[IFormatProvider provider]);
Parse vs ParseExact:
Parse() - assumes the data is valid and does its best to fit it into the type, forcing things that seem vaguely ridiculous when a developer has a chance to invoke common sense.
ParseExact() - only allows the exact format specified and will throw on any variation.
Source on Parse vs ParseExact
There are many ways to convert text to a DateTime, try this one:
DateTime txtMyDate =
DateTime.ParseExact(txtDate.Text, "M/d/yyyy", CultureInfo.InvariantCulture);
Edit: forgot the culture info argument
Use DateTime.ParseExact to extract your date value from a formated date string:
DateTime dateValue =
DateTime.ParseExact(stringDateValue, "M/d/yyyy",
CultureInfo.InvariantCulture);
Try
DateTime instance = DateTime.Parse( txtDate.Text ) ;
which is [somewhat] flexible about what it will accept. Alternatively, DateTime.ParseExact() will give you move control over the conversion.

How to convert data into DateTime object

How do I convert a date string, in the general form of "ccyymmdd" in to a DateTime object in C#?
For example, how would I convert "20100715" in to a DateTime object.
Please - No RTFM links to Microsoft Tech Docs.
Many Thanks...
using System.Globalization;
DateTime.ParseExact("20100715", "yyyyMMdd", CultureInfo.InvariantCulture);
var dt = DateTime.Parse("your date string").ToString("yymmdd");
I don't think cc is a valid date formatting option?
As Richard points out, you can also use DateTime.ParseExact which allows you to use culture information for the parsing, or you can use DateTime.TryParseExact which is the same as DateTime.ParseExact, but if there is an exception then a null date is returned rather then an exception being raised.
EDIT:
The question has been updated so that a DateTime is specifically returned. In that case you can omit the .ToString() part of my answer. Calling DateTime.Parse() will return a DateTime object. When getting the date value via ToString(), simply pass the required formatting string to get the date in the desired format.
Cheers.
Jas.
Take a look at this and this
DateTime.Parse();
DateTime.ParseExact();
And worth a mention
DateTime.TryParse();
If your date string is already sanitized (Borrowed from Mike's answer):
DateTime dt = DateTime.ParseExact("20100715", "yyyyMMdd", CultureInfo.InvariantCulture);
Otherwise:
DateTime dt;
if (!DateTime.TryParseExact("20100715", "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
// Handle bad date
}
System.DateTime.Parse(yourDateString)
You might have to manipulate your string to a format that the method can handle first.
See http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
for more info
I'm not sure what the "cc" part is, but there are a few options.
DateTime.Parse(string) may be able to convert the string, but if the string is in a non-standard format you may have to do some pre-conversion first.

Categories

Resources