

In this article, I refer to this technique as datepart-based.Īs long as the datediff-based and datepart-based expressions are completely clear to you, we can proceed. For example, to calculate the weekday of today's date, assuming Monday as the first day of the week, you use the expression SELECT DATEPART(weekday, GETDATE() + - 1) Īgain, if today happens to be a Tuesday, the above expression would return 2. The expression I used added days to the event date and subtracted a constant representing the logical first day of the week that you want to use. In this article, I refer to this technique as datediff-based.Īnother technique I presented was based on neutralizing the impact of the DATEFIRST setting on the DATEPART calculation.

If today happens to be a Tuesday, the above expression would return 2. For example, to calculate the weekday of today's date, assuming Monday as the first day of the week, you use the expression SELECT DATEDIFF(day, 0, GETDATE()) % 7 + 1

The expression diff % 7 + 1 produced the weekday number. So 0 represents a date that falls on a Monday, 1 represents a date that falls on a Tuesday, and so on. When converting the integer 0 to a datetime value, you get the base date January 1, 1900, which happens to be a Monday. The base date had the same weekday as the one you wanted to set as the logical first day of the week (e.g., the string 19000101 or the integer 0 for Monday). One technique was based on calculating the offset in terms of days between a base date and the event date (call it diff). As a reminder, I presented two techniques to calculate a language-independent weekday number. Now, I want to offer some techniques for calculating the date of the last/next occurrences of a weekday related to a specific date (call it the event date)-for example, calculating the date of the most recent Monday related to today.Īll the techniques I present in this article will be based on the calculations of a language-independent weekday number, which I discussed last month. I presented techniques for separating the date and time parts of a datetime value, returning the first/last day of the month, and calculating a language-independent weekday number. The subtract method is demonstrated later in this chapter, with the resulting Timespan object used to output the number of milliseconds between the start and end time of a set of commands.In my February and March columns, I talked about challenges related to datetime calculations. Another key feature of the Date type is the capability to subtract dates in order to determine a difference between them. As noted earlier, primitive values can be assigned directly within your code, but many developers seem unaware of the format for doing this with dates.
#Date of mail convert to datetime in visual basic code
Running this code results in the output shown in Figure 2-7. TextBox1.Text &= DateTime.UtcNow() & Environment.NewLine Dim dtString = # TextBox1.Text &= dtString.ToLongDateString() End Sub Code snippet from Forml TextBox1.Text &= dtToday.ToShortDateString & Environment.NewLine TextBox1.Text = dtNow & Environment.NewLine Private Sub Dates() Dim dtNow = Now() Dim dtToday = Today() You can use these shared methods to initialize your classes, as shown in the following code sample: These methods can be used to initialize a Date object with the current local date, or the date and time based on Universal Coordinated Time (also known as Greenwich Mean Time), respectively. This method has not been changed from Visual Basic 6.0, but the Today and utcNow methods have been added. For the DateTime structure, the Now method returns a Date value with the local date and time. The concept of shared methods is described in more detail in the next chapter, which covers object syntax, but, in short, shared methods are available even when you don't create an instance of a class. Visual Basic also provides a set of shared methods that provides some common dates. The ToOADate and FromOADate methods support backward compatibility during migration from previous versions of Visual Basic. Note that internally Visual Basic no longer stores a date value as a Double however, it provides key methods for converting the current internal date representation to the legacy Double type. You can, in fact, declare date values using both the DateTime and Date types. The Visual Basic Date keyword has always supported a structure of both date and time.
