Parsing Datetime - vb.net

I have a date time as a string, eg. "2010-08-02", I'm trying to convert it to UTC with the following code snippet
DateTime.ParseExact("2010-08-02Z", "yyyy-MM-ddZ", CultureInfo.InvariantCulture)
When I print to the console, I get the following: 8/1/2010 5:00:00 PM.
Is there a reason why the date shows up as the date before the date I'm trying to parse? I could just add a day to this to advance to the original day, but I wanted to see if there's anything I'm doing wrong in the formatting that's causing this.

EDIT: I had a mixture of being correct and not :)
It's showing you the local time represented by the UTC string. It's annoying that DateTime doesn't make this sort of thing clear, IMO. Additionally, I don't think you want to use 'Z' as the format specifier for the time zone; that's not actually a valid format specifier; it should be 'z', - but that's meant for things like "+01:00". I think you should be using 'K'. Frankly it's not clear, but if you use 'K' it round-trips correctly, certainly ('Z' roundtrips too, but only because it ignores it, treating it as plain text).
You can fix it by just calling ToUniversalTime, or (preferred IMO) specifying DateTimeStyles.AdjustToUniversal as an extra argument:
DateTime dt = DateTime.ParseExact("2010-08-02Z", "yyyy-MM-ddK",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal);

The UTC of midnight for 2010-08-02 happens to be at 5pm on 2010-08-01.

If the original string is just a date in the format "2010-08-02" (without the Z), then why not just:
DateTime.SpecifyKind(
DateTime.ParseExact("2010-08-02",
"yyyy-MM-dd",
CultureInfo.InvariantCulture),
DateTimeKind.Utc);
ParseExact will presumably return a DateTime with Kind = Unspecified, and you can make it UTC or Local as you wish using SpecifyKind.

Related

Date.ParseExact issues

I'm trying to format a date for an API. the desired format is: yyyy-MM-ddTHH:mm:ss.fffffff+HH:mm
(eg. 2022-10-12T09:52:14.1234567+03:00). I'm using Date.ParseExact in the following way:
Date.ParseExact("2022-10-12T09:52:14.1234567+03:00", "yyyy-MM-ddTHH:mm:ss.fffffff+HH:mm", CultureInfo.InvariantCulture)
.
Initially I used 'Now' instead of this string, but then I saw that the string and the desired format have to match. The error I'm getting is 'DateTime pattern 'H' appears more than once with different values.'. Is there a way to avoid that? Also is it possible to use 'Now' in this line?
Thank you
I suspect that you don't have a parse issue, you don't need ParseExact at all. You have a Date and want to return it as a formatted string. Then use ToString and zzz for the utc-offset:
string result = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffffzzz");
Read also: Custom date and time format strings

vb.net datetime.parseExact error message saying invalid format

Hi am trying to use the datetime.parse exact object in vb.net, but I keep getting an error saying invalid format. Here is my statement, could anyone tell me what I am doing wrong??
Dim TimeStart As Date
TimeStart = DateTime.ParseExact("2013.07.15-07:10:02", "yyyy.MM.dd-HH:MM:SS", System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat)
The error is because the seconds must be lower case. Also, you're asking for the month twice. I suspect this is the format string you need:
yyyy.MM.dd-HH:mm:ss
Just as an informational bit, the upper case 'H' means it's looking for 24-hour time, instead of 12-hour time. Lower-case 'h' would mean 12-hour time. Here is the reference for custom datetime format string rules:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Objective-C format date and string

guys
I have two question to ask, they're easy, but bothering me for a while.
I access my .NET test WebService, and it return two parameters to me.
One is a date data just like "/Date(1332399761677+0800)/", and I dont know
how to format it to the normal date format.
Two is a NSString data looks like "12000.00000",and I want to change it to
the format like this:"12000.00".
So,please help me with this two problems. Thank you in advance.
1332399761677 looks like a Unix date, so if you grab that part of the string, use NSString doubleValue to turn it into a double, then use NSDate dateWithTimeIntervalSince1970:, you should be able to get a date. +0800 looks like a timezone, but you wouldn't need the timezone to get the date given a Unix date: 1332399761677 would specify a specific point in time, irrespective of timezones.
As for "12000.00000", you would use doubleValue to make it into a double, make an NSNumberFormatter with its maximumFractionDigits and minimumFractionDigits set to 2, then use stringFromNumber:.

Can anyone translate this dateformat?

I’m having a problem with date formats.
I need to convert the following string into af date object: 2011-09-19T12:23:51Z
And then convert the date object back to a string with this format: 19. september 2011
I can’t figure out what the “T” and “Z” is all about though?
Can anyone help me?
Kind regards
Jesper
The "T" is to separate the date from the time.
The "Z" shows that this is in UTC.
This is a standard (extended) ISO-8601 format date/time string - it should be easy to parse with whatever libraries iOS provides.

Visual basic script that control a date format

I would like to control a string if is in the dd/mm/yyyy format and if the dd number is between 1 and 31 and if mm is between 1 and 12.
In vb.net you can use the IsDate() function to test the validity of a date. This will insure that the day and the month are within the valid range.
You can use the DatePart function:
DatePart("m", date)
DatePart("d", date)
Wait what? Your question is not very clear. Do you have a DateTime and need to output it in a specific format? Are you accepting a string from the user and need to make sure it fits that format? Do you get a string from somewhere else that you need to match for a specific format?
Most of all, why do you care? You shouldn't be dealing with dates as strings, except at the point of interaction with the user or other data source. Inside your program they should be a DateTime type. Assuming you're 'vb.net' tag is correct, the DateTime has handy Parse, TryParse, and ParseExact, and TryParseExact static methods you can use to accept most anything the user could throw at you.