How to convert between unusual time formats golang - vba

I referenced this post: How to convert date format in golang? and have this code on the playground:
https://play.golang.org/p/oNFVlDz9JoF
But I can't seem to get it to work. I've padded the month with a 0 and still no dice. The time I'm trying to convert is 4/20/2018 9:08:34 AM and I want it in this format: 2009-04-20T21:18:44Z
NewLastDate = CDate(LastModified) 'generates 4/20/2018 9:08:34 AM
That's from a VBA script. 'LastModified' is in this format: dddd.ttttt VB/VBA format

Go time formats are interesting, you always have to put your formats in terms of (per the docs) https://golang.org/pkg/time/#Parse
01/02 03:04:05PM '06 -0700
I changed your formats a bit so you could see what is happening. I also would advise checking error returns. It also seems like time.RFC3339 is a good shorthand for what you are looking for on the output side but I may be wrong.
https://play.golang.org/p/eYYdV_UE_Pa

Related

What is this date format called?

What is this date format called: 1YYMMDD
Example: 06/28/1959 outputs as 1590628
Example: 06/28/2019 outputs as 1190628
There is always a stinking 1 in front of it, no matter what.
As #TomNash already mentioned in his comment the 1 seems to be a century marker.
So your date format is CYYMMDD where C could be
0 : for 19xx
1 : for 20xx
This kind of format is not part of ISO 8601!
It looks to be a little bit like an IBM special and maybe AS/400 specific solution for the year-2000-problem.
My personal recommendation is to convert such date formats always to ISO 8601 via an intermediate layer before processing them.
Last but not least please remember to set software that use such outdated formats onto a list, because a solution like the century marker has only moved the year-2000-problem 100 years to the future - so the childs of our grandchilds will have the same problem again ...

NSDataDetector more accuracy (ignore time only)

NSDataDetector is very handy to find different types of dates within a string. (e.g. 2015-03-10 or 10. March 15). This is great but how can I tell NSDataDetector to ignore time patterns only (e.g. "Whatever my text is 2:33 and so on"). NSDataDetector recognises this as - TODAY 2:33
In other words: Can I force NSDataDetector to find full dates only ?
Thanks,
Sascha
You would need to run some post processing. Once you have extracted the list of date found. Loop through each result. Locate the original text used to identify the text and check if the text match your format for instance always a year etc. You can then ignore any of the non full date format.
I am actually looking for the same but could not find anything useful directly built by Apple. Then I come up with this idea. Not idea, but should do the job.

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.

Parsing Datetime

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.

VisualBasic Month function inconsistency

I'm working in a web application using VB.NET. There is also VisualBasic code mixed in it, in particular the Date variable and the Month function of VB.
The problem is this part:
Month("10/01/2008")
On the servers, I get 10 (October) as the month (which is supposed to be correct). On my machine, I get 1 (January) (which is supposed to be wrong).
Two of my colleagues (on their own machines) get different answers, one got 1, the other got 10.
The question is, why is this so?
On my end, I can solve the problem by using .NET's DateTime's Parse (or ParseExact) function to force everything to be "dd/MM/yyyy" format. This works. I'm just wondering why there's an inconsistency.
Extra info: I know the parameter for Month function is supposed to be a Date variable. The code used a string as parameter, and Option Strict was off, and the developers mainly let VB do its own conversion thing. (Legacy code maintenance has a lot of inertia...)
If it helps, the version of Microsoft.VisualBasic.dll on the servers is 7.10.6310.4 (under the Framework folder v1.1.4322). The version on mine (and my 2 colleagues') machine is 7.10.6001.4.
Edit: Regional settings for all machines already set to dd/MM/yyyy format (short date format).
This normally has to do with the regional settings, and more specifically the date/time formats. If you set these formats so that they are all the same on the machines you're testing on, the results should be consistent.
Your idea of using ParseExact is definitely the better solution to go with, IMHO.
This is because the runtime has to convert your given value "10/01/2008" which is indeed a string implicitly to the DateTime datatype.
When converting strings to dates and the other way round, the string format depends on the locale settings of windows.
See this link on msdn.
In this article a way to specify a date literal which is independent of your locale settings:
Just enclose the date with the sign # and specify it in the form mm/dd/yyyy:
So the code
Month(#10/01/2008#)
should give you the answer 10 on any machine.
Ther a two more worarounds given in that msdn article:
1. Use the Format Function with predifned Date/Time Format
To convert a Date literal to the
format of your locale, or to a custom
format, supply the literal to the
Format Function, specifying either
Predefined Date/Time Formats (Format
Function) or User-Defined Date/Time
Formats (Format Function). The
following example demonstrates this.
MsgBox("The formatted date is " &
Format(#5/31/1993#, "dddd, d MMM
yyyy"))
2. Use the DateTime-Class Constructor to construt the right DateTime value
Alternatively, you can use one of the
overloaded constructors of the
DateTime structure to assemble a date
and time value. The following example
creates a value to represent May 31,
1993 at 12:14 in the afternoon.
Dim dateInMay As New
System.DateTime(1993, 5, 31, 12, 14,
0)