.NET Forum / Languages / VB.NET / October 2004
dateTime Manupulations
|
|
Thread rating:  |
Reny J Joseph Thuthikattu - 20 Oct 2004 08:59 GMT 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
Shiva - 20 Oct 2004 09:18 GMT 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
Cor Ligthert - 20 Oct 2004 09:56 GMT 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"
> Hi, > I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a [quoted text clipped - 3 lines] > How do i do that? > Reny Cor Ligthert - 20 Oct 2004 10:31 GMT 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 Ligthert" <notmyfirstname@planet.nl>
> Renny, > [quoted text clipped - 31 lines] >> How do i do that? >> Reny Jay B. Harlow [MVP - Outlook] - 20 Oct 2004 15:29 GMT 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
> Renny, > [quoted text clipped - 31 lines] >> How do i do that? >> Reny Cor Ligthert - 20 Oct 2004 16:22 GMT 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
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com>
> Cor, > Just curious why are you resetting the CurrentCulture to [quoted text clipped - 59 lines] >>> How do i do that? >>> Reny Jay B. Harlow [MVP - Outlook] - 20 Oct 2004 17:21 GMT 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
> Jay, > [quoted text clipped - 34 lines] > > Cor <<snip
Cor Ligthert - 20 Oct 2004 18:03 GMT 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
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com>
> Cor, > It seems to me you have the blinders on a just a little too tight. :-| [quoted text clipped - 81 lines] >> > <<snip Herfried K. Wagner [MVP] - 20 Oct 2004 18:38 GMT "Cor Ligthert" <notmyfirstname@planet.nl> schrieb:
> 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.
 Signature Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/
Cor Ligthert - 20 Oct 2004 19:17 GMT 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
Herfried K. Wagner [MVP] - 20 Oct 2004 19:35 GMT "Cor Ligthert" <notmyfirstname@planet.nl> schrieb:
> 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. >>'CDate' is locale-aware and will return different results for "10/10/1999" [quoted text clipped - 3 lines] > 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/groups?selm=O40B7BUtEHA.636%40TK2MSFTNGP09.phx.gbl>
You either missed the post or ignored it.
 Signature Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/
Cor Ligthert - 20 Oct 2004 20:06 GMT Herfried,
> 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: I did not discuss that, because that was for me not the primairy question, I am from Holland you know, we are for ages dealing with very much cultures, he asked me in a way if I did understand what different cultures mean. Probably you do not know what that means when somebody ask that to a Dutchman. Therefore I found it better not to answer.
However as I stated in this thread, even than is it better to pack his example in a class, and than do all date time or other culture and whatever handling. That will make it much easier and better to maintanance everything than by doing that inline somewhat hidden in a program. He did not answer that as well.
This would be an example.
Get the culture settings from whatever document from whatever page by instance using the clientcertificate.
Set the globalization.culture with that Do all the handling with the XML document Set the globalization setting back to the normal used
This gives me more the idea of good program than inline setting of all the date formats.
However that is not the point where we are discussing about. We are discussing that I say that it is good to use the globalization settings as much as possible and to avoid hard coded date time formats as you have statted and from what I get the idea Jay is supporting you.
Cor
Herfried K. Wagner [MVP] - 20 Oct 2004 20:18 GMT "Cor Ligthert" <notmyfirstname@planet.nl> schrieb:
> Set the globalization.culture with that > Do all the handling with the XML document > Set the globalization setting back to the normal used > > This gives me more the idea of good program than inline setting of all the > date formats. I doubt that this is the best solution. Imagine your class throwing an exception and not setting back the current culture, for some reason. Many methods accept a format information or culture information object in order to use a specific format information for doing their job. All of that can be done without actually changing the culture of the thread the routine is running in.
> However that is not the point where we are discussing about. We are > discussing that I say that it is good to use the globalization settings as > much as possible I agree, and I think that Jay agrees to that too, but that's not always a viable option (see Jay's sample with exchanging XML files).
 Signature Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/
Jay B. Harlow [MVP - Outlook] - 20 Oct 2004 19:55 GMT Cor,
> 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. If you read the original posts, they both state directly or indirectly a "hard coded date format", we (Herfried and I) are not suggesting they do this, we (Herfried and I) are suggesting if they do this do it in the "correct" manner, which is DateTime.ParseExact.
You seem to be suggesting that CDate is the only way and to get it to work, change the CurrentCulture.
I am seriously questioning, why you would change the CurrentCulture to format dates. You offered an explanation, to which I still seriously question why you would think this way.
You haven't really offered any real explanation that I can follow...
I hope you will understand if I don't respond to you on this matter any more, its not that I cannot speak for myself, its that its too tiring and life is too precious to try and explain it to you. :-|
I would ask you wait until tomorrow before responding, instead "walk in another mans shoe" & try the samples we have given, and why we would offer our advice.
Thanks Jay
> Herfried, > [quoted text clipped - 22 lines] > > Cor Cor Ligthert - 20 Oct 2004 20:34 GMT I have said in all those threads that there was nowhere showed that there should be a culture change in the dates. When I write to you that my datestring is in the dd-MM-yy format, does that than means for you that you have to give me directly the sample to change it into the US date.
I have nowhere written for the rest in the previous message, what you said that I wrote, that comes from your mind not mine.
I have given you an example in a previous message in this thread, why using the culture settings was better, as this bellow you did probalby not take the time to read that.
This is in the message you was now answering.
>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. And when you don't understand something from my messages, ask first what you don't understand before you give an answer.
Cor
Jay B. Harlow [MVP - Outlook] - 20 Oct 2004 19:30 GMT 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:
>> 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
> Jay, > [quoted text clipped - 98 lines] >>> >> <<snip Cor Ligthert - 20 Oct 2004 19:46 GMT > Cor, > (shakes head) [quoted text clipped - 10 lines] > 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
Jay B. Harlow [MVP - Outlook] - 20 Oct 2004 19:50 GMT 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
>> Cor, >> (shakes head) [quoted text clipped - 19 lines] > > Cor Cor Ligthert - 20 Oct 2004 20:21 GMT Jay, .
>> 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. It seems that you can give only this kind of abusing answers to me telling what I do not know. A kind of writting what is beneath my level so you have no problem with that from me.
You are avoiding real answers on concrete questions, and now even misquoting me by deleting the sample.
What is wrong with an concrete answer on my question, do you have to agree with that sample maybe?
As I said in the other thread which you never answered like probably this of course, I find the globalization one of the best things from dotNet.
The only thing you tell in this thread is that you and other MVP's know it better and that you do not agree with me, where I say that the globalization settings should be used. And because of that you are telling that the date and time where it is used should be hard formated.
Cor
Rangi Keen - 20 Oct 2004 22:52 GMT This and the previous discussion on date parsing have been very amusing. I think there must be some miscommunication happening somewhere (perhaps a language issue), but I agree that DateTime.ParseExact would be the best method in both these cases.
> Cor, > > And what is wrong with my answer than packed in a sample that would work [quoted text clipped - 29 lines] > > > > Cor Herfried K. Wagner [MVP] - 20 Oct 2004 20:07 GMT > And what is wrong with my answer than packed in a sample > that would work in every culture setting. It would not work with every culture setting... See below...
> MessageBox.Show(CDate("01-JUNE-2004 11:59 PM").Addminutes(1).ToString) \\\ Imports System.Globalization Imports System.Threading . . .
' Fake the user's culture settings... Dim ci As New CultureInfo("en-US") ci.DateTimeFormat.MonthNames = _ New String() { _ "JUNE", _ "B", _ "C", _ "D", _ "E", _ "F", _ "G", _ "H", _ "I", _ "J", _ "K", _ "L", _ "M" _ } Thread.CurrentThread.CurrentCulture = ci . . . MsgBox(CDate("01-JUNE-2004 11:59 PM").AddMinutes(1).ToString()) ///
The result will be a date in January 2004, which is not what the user expects...
The best solution is using 'DateTime.ParseExact' in combination with specifying the date format pattern + the invariant culture.
 Signature Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/
Herfried K. Wagner [MVP] - 20 Oct 2004 15:39 GMT "Cor Ligthert" <notmyfirstname@planet.nl> schrieb:
> 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...
 Signature Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/
Cor Ligthert - 20 Oct 2004 16:31 GMT 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
Herfried K. Wagner [MVP] - 20 Oct 2004 16:36 GMT "Cor Ligthert" <notmyfirstname@planet.nl> schrieb:
> 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.
 Signature Herfried K. Wagner [MVP] <URL:http://dotnet.mvps.org/
Cor Ligthert - 20 Oct 2004 16:42 GMT > I don't know if you are ignorant or just don't understand it. Again an abuse
Jay B. Harlow [MVP - Outlook] - 20 Oct 2004 15:38 GMT 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/default.asp?url=/library/en-us/cpguide/html/cp condatetimeformatstrings.asp
For information on formatting in .NET in general see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cp conformattingtypes.asp
Hope this helps Jay
> Hi, > I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a [quoted text clipped - 8 lines] > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.779 / Virus Database: 526 - Release Date: 19/10/04 Jay B. Harlow [MVP - Outlook] - 20 Oct 2004 19:25 GMT 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)
> Hi, > I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a [quoted text clipped - 8 lines] > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.779 / Virus Database: 526 - Release Date: 19/10/04
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|