Date Variable Changes

  • Thread starter Thread starter AGP
  • Start date Start date
A

AGP

VB2005
I'm trying to understand the Date variable that i use in one of forms. The
user can input a date/time into a textbox and no matter how they write it,
the variable is understood to be in Zulu. The pronme that i have right now
is that if the user inputs the time as "4/29/2009 11:00Z" when I pass this
variable to another function that issues a SQl the date gets translated to a
local time and the time that I really need is juts a zulu time. Not sure
that comes out right. So say the user inputs "4/29/2009 11:00Z" or
""4/29/2009 11:00" both are understood to be in zulu time and thats what i
want to pass to my function. Is there a way that i can write my fucntion to
interpret that value as zulu and only zulu time?

AGP
 
Code from MSDN

\\\
' Change the Kind property of the current moment to
' DateTimeKind.Utc and display the result.
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc)
Display("Utc: .............", myDt)
///
Zulu time is a non official name as Z time which stands for Zero and is
translated to the English character description Zulu

This is now called UTC while it was in past GMT (Normally used in Europe)

Cor
 
AGP said:
VB2005
I'm trying to understand the Date variable that i use in one of forms. The
user can input a date/time into a textbox and no matter how they write it,
the variable is understood to be in Zulu. The pronme that i have right now
is that if the user inputs the time as "4/29/2009 11:00Z" when I pass this
variable to another function that issues a SQl the date gets translated to
a local time and the time that I really need is juts a zulu time. Not sure
that comes out right. So say the user inputs "4/29/2009 11:00Z" or
""4/29/2009 11:00" both are understood to be in zulu time and thats what i
want to pass to my function. Is there a way that i can write my fucntion
to interpret that value as zulu and only zulu time?

http://msdn.microsoft.com/en-us/library/system.datetime.specifykind.aspx


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4044 (20090430) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
Is that function converting the date that you pass to it to local time, or
is it assuming that the date you are passing to it is already a local time
and storing it with a time zone adjustment? If that's what is happening
then when you retrieve that stored time as a local time you will have a time
zone adjustment, as if the original time was entered as a local time. You
could fix it by converting the zulu time entered by the user into a local
time before passing it to that function.

The DateTime.TryParse method allows you to convert a date string into a
DateTime variable. Use the .SpecifyKind method and .ToLocalTime to get the
equivalent local time, and then use the .ToString method with a formattng
pattern to produce a string date and time in the format expected by that
function.

There's an example here:
http://msdn.microsoft.com/en-us/library/system.datetime.kind.aspx
 
Thanks for the help. Much appreciated. I unbderstand the naming convention.
The problems that I have is that in my industry it is VERY ingrained to use
the "Z" marker to denote UTC date/time and we also very commonly call it
Zulu time. In my program I have a textbox where the user inputs the zulu
date/time but im 100% sure that some users will add the Z and some will not
since that is how all of our other processes work. So with that in mind here
is what i have for the possibilities that the user will input into the
textbox

Dim sWhatDate As String
'sWhatDate = "2009-04-25 13:00"
sWhatDate = "2009-04-25 13:00Z"

Dim dtMyDate As Date = DateTime.SpecifyKind(CDate(sWhatDate),
DateTimeKind.Utc)
Debug.WriteLine(dtMyDate)

The first date will result in
4/25/2009 1:00:00 PM
and the second date will result in
4/25/2009 8:00:00 AM

Again, I do understand that there is some internal conversion going on. In
the end every user will understand that the input is to be in Zulu time but
its how they actually write it that is the problem for me when I assign it
to my date variable.

AGP
 
AGP said:
Thanks for the help. Much appreciated. I unbderstand the naming
convention. The problems that I have is that in my industry it is
VERY ingrained to use the "Z" marker to denote UTC date/time

Why not replace the Z with UTC in the string (or whatever makes parsing the
datetime happy)?

Andrew
 
AGP said:
Thanks for the help. Much appreciated. I unbderstand the naming
convention. The problems that I have is that in my industry it is
VERY ingrained to use the "Z" marker to denote UTC date/time and we
also very commonly call it Zulu time. In my program I have a textbox
where the user inputs the zulu date/time but im 100% sure that some
users will add the Z and some will not since that is how all of our
other processes work. So with that in mind here is what i have for
the possibilities that the user will input into the textbox

Dim sWhatDate As String
'sWhatDate = "2009-04-25 13:00"
sWhatDate = "2009-04-25 13:00Z"

Dim dtMyDate As Date = DateTime.SpecifyKind(CDate(sWhatDate),
DateTimeKind.Utc)
Debug.WriteLine(dtMyDate)

The first date will result in
4/25/2009 1:00:00 PM
and the second date will result in
4/25/2009 8:00:00 AM

Again, I do understand that there is some internal conversion going
on. In the end every user will understand that the input is to be in
Zulu time but its how they actually write it that is the problem for
me when I assign it to my date variable.


I think the code you need is

dtMyDate = CDate(sWhatDate)

Select Case dtMyDate.Kind
Case DateTimeKind.Local
dtMyDate = d1.ToUniversalTime
Case DateTimeKind.Unspecified
dtMyDate = DateTime.SpecifyKind(dtMyDate , DateTimeKind.Utc)
End Select

because:

- If no "Z" has been entered, the kind has not been specified by the user.
That's correctly indicated by Kind=Unspecified. But in this case, _you_ know
the kind. It's UTC. You can add this piece of information by calling
SpecifiyKind.

- If "Z" has been entered, it's correctly recognized as zulu time. An
additional, internal conversion to local time is done. As you want to store
it as UTC, you can call ToUniversalTime.


BTW, there's also the DateTimeOffset type - but I didn't look if it can help
you here.


Armin
 
Hi,

I think that you simply should not allow that Z or simple filter it by
something as txt.Replace("Z","")

I tried this
\\\
Debug.WriteLine((DateTime.SpecifyKind(CDate(DateTime.Now.ToString("2009-04-3013:00Z")), DateTimeKind.Utc)).ToLocalTime) Debug.WriteLine((DateTime.SpecifyKind(CDate(DateTime.Now.ToString("2009-04-30 13:00")), DateTimeKind.Utc)).ToLocalTime)///CorThe first gives me backThere is nothing magic going on, your users have to know if they enter"AGP" <[email protected]> wrote in messagenews:[email protected]...> Thanks for the help. Much appreciated. I unbderstand the namingconvention. The problems that I have is that in my industry it is VERYingrained to use the "Z" marker to denote UTC date/time and we also verycommonly call it Zulu time. In my program I have a textbox where the userinputs the zulu date/time but im 100% sure that some users will add the Zand some will not since that is how all of our other processes work. So withthat in mind here is what i have for the possibilities that the user willinput into the textbox>> Dim sWhatDate As String> 'sWhatDate = "2009-04-25 13:00"> sWhatDate = "2009-04-25 13:00Z">> Dim dtMyDate As Date = DateTime.SpecifyKind(CDate(sWhatDate),DateTimeKind.Utc)> Debug.WriteLine(dtMyDate)>> The first date will result in> 4/25/2009 1:00:00 PM> and the second date will result in> 4/25/2009 8:00:00 AM>> Again, I do understand that there is some internal conversion going on. Inthe end every user will understand that the input is to be in Zulu time butits how they actually write it that is the problem for me when I assign itto my date variable.>> AGP>>> "Cor Ligthert[MVP]" <[email protected]> wrote in messageCode from MSDN>>>> \\\>> ' Change the Kind property of the current moment to>> ' DateTimeKind.Utc and display the result.>> myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Utc)>> Display("Utc: .............", myDt)>> ///>> Zulu time is a non official name as Z time which stands for Zero and istranslated to the English character description Zulu>>>> This is now called UTC while it was in past GMT (Normally used in Europe)>>>> Cor>>>>>> "AGP" <[email protected]> wrote in messageVB2005>>> I'm trying to understand the Date variable that i use in one of forms.The user can input a date/time into a textbox and no matter how they writeit, the variable is understood to be in Zulu. The pronme that i have rightnow is that if the user inputs the time as "4/29/2009 11:00Z" when I passthis variable to another function that issues a SQl the date gets translatedto a local time and the time that I really need is juts a zulu time. Notsure that comes out right. So say the user inputs "4/29/2009 11:00Z" or""4/29/2009 11:00" both are understood to be in zulu time and thats what iwant to pass to my function. Is there a way that i can write my fucntion tointerpret that value as zulu and only zulu time?>>>>>> AGP>>>>>>>
 
Armin Zingler said:
I think the code you need is

dtMyDate = CDate(sWhatDate)

Select Case dtMyDate.Kind
Case DateTimeKind.Local
dtMyDate = d1.ToUniversalTime
Case DateTimeKind.Unspecified
dtMyDate = DateTime.SpecifyKind(dtMyDate , DateTimeKind.Utc)
End Select

because:

- If no "Z" has been entered, the kind has not been specified by the user.
That's correctly indicated by Kind=Unspecified. But in this case, _you_
know
the kind. It's UTC. You can add this piece of information by calling
SpecifiyKind.

- If "Z" has been entered, it's correctly recognized as zulu time. An
additional, internal conversion to local time is done. As you want to
store
it as UTC, you can call ToUniversalTime.


BTW, there's also the DateTimeOffset type - but I didn't look if it can
help
you here.


Armin

I like this approach and learned something. The date entry doesnt have to be
completely bullet proff but at least the 2 possibilities that i can think of
are accounted for.

Thanks

AGP
 
AGP,

I had made a piece of code, however I get the same results as you.

It is not wrong, but difficult to use.

In fact my first part of the message stays.

Cor
 
AGP,

I had made a piece of code, however I get the same results as you.

It is not wrong, but difficult to use.

In fact my first part of the message stays.

Cor


yes it is difficult if you are not used to the auto conversion. As
long as you understand it going in then no problem but i just haunt
worked a lot with dates in .net. I took your suggestions as well as
the others and I think i came up with a workable solution.

Thanks

AGP
 
Back
Top