dateTime Manupulations

  • Thread starter Thread starter Reny J Joseph Thuthikattu
  • Start date Start date
R

Reny J Joseph Thuthikattu

Hi,
I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a
miniute to it.How can i do that?
by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004
12:00 AM'
How do i do that?
Reny
 
Convert the date/time string to DateTime type using DateTime.ParseExact()
and then call .AddMinutes(1).

HTH.

Hi,
I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a
miniute to it.How can i do that?
by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004
12:00 AM'
How do i do that?
Reny
 
Renny,

This sample should do it

\\\
Public Class Main
Public Shared Sub Main()
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo("en-AU")
'When you are not in the culture with this settingtis

MessageBox.Show(CDate("01-JUNE-2004 11:59 PM").AddDays(1).ToString)
'The above row is the only thing what is needed when it is in your own
culture setting

'And set the culture back when it was not your own culture
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture
End Sub
End Class
///

I hope this helps?

Cor



"Reny J Joseph Thuthikattu"
 
Doh,
MessageBox.Show(CDate("01-JUNE-2004 11:59 PM").AddDays(1).ToString)
MessageBox.Show(CDate("01-JUNE-2004 11:59 PM").Addminutes(1).ToString)

Of course

Cor
 
Cor,
Just curious why are you resetting the CurrentCulture to InstalledUICulture?

Why are you arbitrarily picking a Culture to get a specific date format?

Wouldn't using DateTime.ParseExact be better? In that you can specify a
specific format, plus you don't run the risk of resetting CurrentCulture
that may have been explicitly been set to something other then
InstalledUICulture elsewhere in your program?

Honestly, I am sincerely interested in knowing what you don't understand
about DateTime.ParseExact?

Using DateTime.ParseExact would be:

Const format As String = "dd-MMMM-yyyy hh:mm tt"
Dim s As String = "01-JUNE-2004 11:59 PM"

Dim d As DateTime = DateTime.ParseExact(s, format, Nothing)

Notice that its a single line & there is no chance of changing the
CurrentCulture that may have explicity been set elsewhere in the program.

Just curious
Jay
 
Reny,
Is your variable a string or DateTime variable?

Do you want help converting a date from a string, is your date in a specific
format or is that how Windows Formats your dates?

Am I reading your message correctly, you want to round a date up to the next
day?

I would use something like:
Dim s As String = "01-JUNE-2004 11:59 AM"

' converts a string in a specific
Const format As String = "dd-MMMM-yyyy hh:mm tt"
Dim d As DateTime = DateTime.ParseExact(s, format, Nothing)

Dim noon As TimeSpan = TimeSpan.FromHours(12)

' Rounds a date to the nearest day
If d.TimeOfDay.CompareTo(noon) > 0 Then
d = d.Date.AddDays(1)
Else
d = d.Date
End If

Debug.WriteLine(d.ToLongDateString())
Debug.WriteLine(d.ToLongTimeString())



For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp

Hope this helps
Jay
 
Cor Ligthert said:
Threading.Thread.CurrentThread.CurrentCulture = _
New Globalization.CultureInfo("en-AU")
[...]
'And set the culture back when it was not your own culture
Threading.Thread.CurrentThread.CurrentCulture = _
Globalization.CultureInfo.InstalledUICulture

It seems that you missed Jon Skeet's comment about this code in the previous
thread...
 
Jay,

I just following the advice from the VBNet pages to use the Convert
Functions from which I understand it is advices to use them because they
give the best overall performance.

Although when those advices were not given do I find them very clear to use
and easy. I find them one of the best extra parts of VBNet.

Because of all that doing, from Herfried and others who say that when
someone who asks how to set a date and time, and than gives with that a
format string, particulary wants to use change a format not in his own
local format I give now that other situation.

In my opinion are those datestring information more given as extra
information for us (what format the OP is using) than that he directly wants
to convert the format. I did not by accident give the Australian format when
I saw the time of the message. However that was a gues.

Beside that can I only think of one situation where it is needed to change
the dateandtime given in a culture and that is with a webformpage or with
any other document where that is in.

Than it is better in my opinion to set in the class that processes that
first the right language/culture and set that back at the end of the class.
Doing that for every instruction inside will give in my idea more dangerous
code when there is maintanance.

That has nothing to do with understanding. Sometimes I get the idea that you
think that I write things because of lack of knowledge.

For me are rules as 80/20 and "keep it simple" very important. A program
have to be made however mostly as well will there be maintenance on it.

I hope that you can share my opinion above and understand why I am telling
this about globalization.

Cor
 
Herfried,

The mainpart of the message was.

MessageBox.Show(CDate("01-JUNE-2004 11:59 PM").AddDays(1).ToString)
'The above row is the only thing what is needed when it is in your own
culture setting

Please do not express misquote me what this is obvious

I see that as abuse.

Cor
 
Cor Ligthert said:
The mainpart of the message was.

MessageBox.Show(CDate("01-JUNE-2004 11:59 PM").AddDays(1).ToString)
'The above row is the only thing what is needed when it is in your own
culture setting

This doesn't make it work better.
Please do not express misquote me what this is obvious

I didn't misquote anything.
I see that as abuse.

I don't know if you are ignorant or just don't understand it.
 
Cor,
It seems to me you have the blinders on a just a little too tight. :-|

In other words you are only seeing CDate, where as you might want to look to
the side & see DateTime.ParseExact also!

I agree using the VB Convert Functions in most cases is the way to go.
However like any rule there are exceptions. As has been demonstrated,
converting a DateTime with a specific format is one of those exceptions.

An analogy might be a Carpenter. Does a (good) carpenter have a single
hammer? No, they have 2 or 3 kinds of hammers, as each is needed depending
on what he is doing. Does he have a single screwdriver? No, he has standard
(or slotted) and Phillips, plus possible a whole slew of others. Does he
have a single size of screwdriver? No he has #0 thru #4 sizes based on the
requirements of the specific job.

The Carpenter has different tools & sizes & shapes of the "same" tool, as
each is required for a specific job. Same with CDate & DateTime.ParseExact.
Most of the time CDate (a #2 slotted screwdriver) is fine, however sometimes
you need a specialized tool, such as DateTime.ParseExact (a #0 slotted
screwdriver).

For the life of me, I really don't understand why you don't see this.
Reading Reny & z.'s earlier question both stated, in a specific format,
which to a number of developers myself included, suggests that
DateTime.ParseExact with a specific format string is the correct method in
this instance.

I hope you realize I am not saying don't use CDate, as I use it where CDate
makes sense.

What I am saying is that CDate does not make sense in this case. For some
reason you are not accepting this, from Herfried, Jon, myself or others. I
really don't know why, or how we can better explain it.

In closing, I sincerely hope you will take the time & reread both of these
threads and make a concerted effort to understand why a number of us may be
saying that in these two specific cases using DateTime.ParseExact might be
better then relying solely on CDate. Hopefully you will realize that the
blinders are just a little too tight.

Hope this helps
Jay


Cor Ligthert said:
Jay,

I just following the advice from the VBNet pages to use the Convert
Functions from which I understand it is advices to use them because they
give the best overall performance.

Although when those advices were not given do I find them very clear to
use and easy. I find them one of the best extra parts of VBNet.

Because of all that doing, from Herfried and others who say that when
someone who asks how to set a date and time, and than gives with that a
format string, particulary wants to use change a format not in his own
local format I give now that other situation.

In my opinion are those datestring information more given as extra
information for us (what format the OP is using) than that he directly
wants to convert the format. I did not by accident give the Australian
format when I saw the time of the message. However that was a gues.

Beside that can I only think of one situation where it is needed to change
the dateandtime given in a culture and that is with a webformpage or with
any other document where that is in.

Than it is better in my opinion to set in the class that processes that
first the right language/culture and set that back at the end of the
class. Doing that for every instruction inside will give in my idea more
dangerous code when there is maintanance.

That has nothing to do with understanding. Sometimes I get the idea that
you think that I write things because of lack of knowledge.

For me are rules as 80/20 and "keep it simple" very important. A program
have to be made however mostly as well will there be maintenance on it.

I hope that you can share my opinion above and understand why I am telling
this about globalization.

Cor
<<snip>>
 
Jay,

Your message shows only that you completly did not read my message or
completly do not understand what is the situation where it is in real
business needed to convert a datetimestring to another culture.

In none of the threads I have read explicitly that there was needed a
datetime conversion between strings, however it was a datetime conversion
between a datetime value and a string value.

So show me first where that culture conversion is needed before you start to
tell that for conversion between a string and a date a CDate is not the best
choise.

Cor
 
Cor Ligthert said:
In none of the threads I have read explicitly that there was needed a
datetime conversion between strings, however it was a datetime conversion
between a datetime value and a string value.

In /both/ threads Jay is referring two the question was about /parsing/ a
date that is represented as a string. This is a common scenario when dates
are stored in a specific format in a text file, for example. The questions
was not about a conversion between a 'Date' and a 'Date', this would not
make much sense.
So show me first where that culture conversion is needed before you start
to tell that for conversion between a string and a date a CDate is not the
best choise.

'CDate' is definitely not the best choice when parsing dates represented as
strings in a specific format. All I can do is stating again what I already
said: I don't know if you are ignorant or just don't understand it. It
seems that you ignore every explanation, so further discussion is rather
useless.
 
Herfried,

Are you Jay's advocacy. When Jay can not speak for himself than show me in
those threads that the OP's exaplictly wanted a conversion between a
culture. I saw only conversions between types.

(Where in the last thread it could be the reason that there was another
datetime setting, what however could be as well because it was setted wrong
on the computer. The OP did never answer that question from me where I asked
what the used format was in his country)

This is your sentence in the thread before that.
'CDate' is locale-aware and will return different results for "10/10/1999"
on machines with different date formats.

When you and Jay want to hard code a dateformat in a program, it is fine, I
keep telling in this newsgroup to use as posible as can the globalization
settings.

When it was an obvious question about a culture change from a date, than I
would have said nothing about using the datetime.parse or any other command
that was sufficient.

This is again nothing else than I am all the time writing.

Cor
 
Reny,
In my discussion with Cor, I just realized that you asked how to add a
minute to a DateTime.

My other post shows how to parse & format a string. As Cor showed, you can
use DateTime.AddMinutes to add a minute to a DateTime.

Given my other example:

d = d.AddMinutes(1)

If you want to return d to a string in the same format you can use:

Const format As String = "dd-MMMM-yyyy hh:mm tt"
Dim s As String = d.ToString(format)
 
Cor,
(shakes head)

Oh! really, then we must be reading different messages!

Reny stated:
<quote>
Hi,
I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a
miniute to it.How can i do that?
by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004
12:00 AM'
How do i do that?
Reny
</quote>

Notice the quoted string, a specific format for a date & time. Also notice
he says he wants to take a variable (which may be a string that happens to
have a date in it), and do something with it. If he does have a string, he
will need to convert that string into a DateTime.

Z. f. stated:
<quote>
HI,
i have string in format
dd/mm/yyyyy hh:mm:ss
and giving this as an input to DateTime.Parse gives a string was not
recognized as a valid date time format string error.
how do i make the parse method to accept formatting that i need.
if i do console.writeLine DateTime.Now.ToString it gives the format:
YYYY-MM-DD HH:MM:SS

TIA, Z.
</quote>

Notice the second line, a specific format for a date & time, also notice he
states "how do i make the parse method".
In none of the threads I have read explicitly that there was needed a
datetime conversion between strings, however it was a datetime conversion
between a datetime value and a string value.

In zf's case he stated explicitly "I have a string".

In Reny's case you are correct he simply states "I have a variable", I
noticed that both your & my example assume a string variable as input, it
could very well be that his variable is a datetime variable and in both
cases conversion is not necessary (which is why I asked in my original
response to him).

I noticed your posts don't discuss formatting the output per se either,
instead you rely on changing the currentculture...
So show me first where that culture conversion is needed before you start
to tell that for conversion between a string and a date a CDate is not the
best choise.
That's the point, there is no culture conversion, as a specific date time
format is given, a string is give, and the OP wants a DateTime value
returned. CDate using the current culture, hence there is a culture
conversion. As Jon & I discussed yesterday DateTime.ParseExact does not
involve any culture when you give it a specific format!

As I stated:
Hope this helps
Jay
 
Cor Ligthert said:
Are you Jay's advocacy.

In this case yes, because he is right.
When Jay can not speak for himself than show me in those threads that the
OP's exaplictly wanted a conversion
between a culture. I saw only conversions between types.

I /never/ talked about conversions between cultures. The OPs were asking
for a *converstion of a string representing a date in a specific format to a
'Date'*. The problem is completely independent from cultures. The problem
is about parsing a date string without caring about the culture, but by
providing a format string.
This is your sentence in the thread before that.

When you and Jay want to hard code a dateformat in a program,
it is fine, I keep telling in this newsgroup to use as posible as
can the globalization settings.

That's something different we are not talking about, because the OP doesn't
need that. Jay gave an excellent sample where parsing date strings in a
specific format is a requirement:

<URL:http://www.google.de/[email protected]>

You either missed the post or ignored it.
 
Cor,
(shakes head)

Oh! really, then we must be reading different messages!

Reny stated:
<quote>
Hi,
I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a
miniute to it.How can i do that?
by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004
12:00 AM'
How do i do that?
Reny

And what is wrong with my answer than packed in a sample that would work in
every culture setting. (Because of all discussions I tested it of course
before)

MessageBox.Show(CDate("01-JUNE-2004 11:59 PM").Addminutes(1).ToString)

Cor
 
Cor,
And what is wrong with my answer than packed in a sample that would work
in every culture setting. (Because of all discussions I tested it of
course before)
I answered you in the form of questions earlier, hoping you would think for
a hour or so & come to the same conclusions as the rest of us.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top