<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Date Class &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/date-class/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Mon, 15 Jun 2020 20:15:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Get Better Dates with the New Xojo Framework</title>
		<link>https://blog.xojo.com/2017/11/04/get-better-dates-with-the-new-xojo-framework/</link>
		
		<dc:creator><![CDATA[Geoff Perlman]]></dc:creator>
		<pubDate>Sat, 04 Nov 2017 10:00:33 +0000</pubDate>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Date Class]]></category>
		<category><![CDATA[Xojo Framework]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=3337</guid>

					<description><![CDATA[If you've written code for long enough, you've probably had bugs where you failed to consider some of the differences between two dates. This example deals only with time zones but the Date and DateInterval classes also handle leap years for you as well. The changes to date and time handling in the New Xojo framework are just a few examples of why we have created it and why it's worth your time to start using it.]]></description>
										<content:encoded><![CDATA[<p><a href="https://imgs.xkcd.com/comics/supervillain_plan.png"><img fetchpriority="high" decoding="async" class="aligncenter" src="https://imgs.xkcd.com/comics/supervillain_plan.png" alt="" width="372" height="492" /></a></p>
<p>Time zones can cause confusion even for super villains. But the Xojo framework has got you covered, whether you are out to do good or evil.</p>
<p><span id="more-3337"></span></p>
<p>If you&#8217;ve written code for long enough, you&#8217;ve probably had bugs where you failed to consider some of the differences between two dates.</p>
<p>Consider the following <em>Classic Xojo framework</em> example that adds 3 hours to a specific date:</p>
<pre>Dim d As New Date(2017, 11, 5)
d.Hour = d.Hour + 3
MsgBox(Str(d))</pre>
<p>Easy enough, right? Unfortunately, while code like this will provide the correct result most of the time, it won&#8217;t this time because on November 5th, 2017 here in the United States, Daylight Savings ends. As a result, the value return will be off by an hour.</p>
<p><em>API 2.0</em> solves this problem by introducing the DateTime and DateInterval classes which is used to add or subtract time and takes into account time zones:</p>
<pre>Var d As New DateTime(2017, 11, 5, 0, 0, 0, 0, TimeZone.Current)
Var di As New DateInterval(0, 0, 0, 3)
d = d + di
MessageBox(d.ToString)</pre>
<p>This example creates a new date and provides the current time zone then creates a DateInterval and assigns it 3 hours. The DateInterval is then added to the date and the result displayed. This code will always be correct because time zones are being considered. This one is pretty straightforward because it&#8217;s only dealing with US Daylight Savings Time but imagine if one date was tied to someone in the US and another to someone in Europe. Not only do you have different time zones but Daylight Savings Time starts and stops on different dates between the US and Europe. The new DateTime and DateInterval classes in API 2.0 handle all of that complexity for you. Because the new DateTime class understands time zones, using TimeZone.Current for example, means you don&#8217;t even have to think about it at all. Your calculations will just work no matter where the user is in the world.</p>
<p>The API 2.0 solution requires one extra line of code. However, that one extra line gets you code that will always produce the correct result. The Date class in the Classic Xojo framework will be right most of the time but when it&#8217;s wrong, you may wrack your brain for a while trying to figure out why. That extra line of code is worth every keystroke.</p>
<p>This example deals only with time zones but the DateTime and DateInterval classes also handle leap years for you as well. That&#8217;s another example where it would be very easy to miss the fact that a leap year occurred and get the wrong result.</p>
<p>The changes to date and time handling in API 2.0 are just a few examples of why we have created it and why it&#8217;s worth your time to start using it.</p>
<p>Note: This post was updated for API 2.0 on June 15th, 2020.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Dates…What&#8217;s the difference?</title>
		<link>https://blog.xojo.com/2017/10/31/dateswhats-the-difference/</link>
		
		<dc:creator><![CDATA[Javier Menendez]]></dc:creator>
		<pubDate>Tue, 31 Oct 2017 07:43:09 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[AprendeXojo]]></category>
		<category><![CDATA[Date Class]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=3487</guid>

					<description><![CDATA[The Xojo.Core.Date and Xojo.Core.DateInterval classes make it really easy to get the difference between two given dates. But what if you want to get the same function for your current code based on the classic Date class?]]></description>
										<content:encoded><![CDATA[<p>Some days ago (or long, long ago, depending when do you read these lines) I received an email from a developer that was porting code from his old <a href="http://developer.xojo.com/migrating-from-visual-basic"><b>VisualBasic</b></a> domain to the native, multi-platform Xojo. He asked me how can to get the difference between two dates? I&#8217;m pretty sure that most of you will have the answer, but I told him he&#8217;ll need <a href="http://developer.xojo.com/xojo-core-date"><b>Xojo.Core.Date</b></a> and <a href="http://developer.xojo.com/xojo-core-dateinterval"><b>Xojo.Core.DateInterval</b></a>. If you want to know how easy it is or how to get the same result for all your code based on the <em>old</em> date class, then I invite you to continue reading…<span id="more-3487"></span></p>
<h2>Use the new Framework, Luke</h2>
<p>The <strong>Xojo.Core.Date</strong> and <strong>Xojo.Core.DateInterval</strong> classes under the <em>new</em>, Xojo framework make it really easy to get the difference between two given dates. The code you&#8217;ll need for this is as easy as this:</p>
<pre>Using Xojo.Core
Dim dateA As date = New Date(2017,10,23, TimeZone.current)
Dim dateB As date = New Date(2017,11,23, TimeZone.Current)
Dim result As DateInterval = dateB - dateA
MsgBox result.Years.ToText+ " " + result.Months.ToText + " " + result.Days.ToText</pre>
<p>The key here is in the use of the subtract operator that gives as result a <b>TimeInterval</b> instance. Thus, accessing the properties of this instance will get the years, months and days elapsed between the two compared dates. Take into account the importance of the order of your Date operands when used with the subtract operator. I mean, if the rightmost date is higher than the leftmost date, then probably you will get some negative values for the properties. In some cases, this will be the expected behaviour for the logic of your app… other times, you will prefer to check this before getting the DateInterval instance in order to get proper values.</p>
<h2>Date difference for existing code</h2>
<p>But, what if you want to get the same function for your current code based on the <b>date</b> class? Here you can opt for several options. The one we are going to put in practice is based in the use of the Xojo <b>Module</b> in combination with the <a href="http://developer.xojo.com/extends"><b>Class Extension</b></a> OOP feature. In fact, Class Extension is the way we can <em>extend</em> the functionallity of an existing Class. Straight, right? So, in order to achieve this the first step, add a new <b>Module</b> to a Xojo project and name it using the <b>Inspector Panel</b> (for example, &#8220;Extents&#8221;). Then, select the newly created module and add a new <b>Method</b> to it using the following signature:</p>
<ul>
<li><b>Method Name</b>: <code>Difference</code></li>
<li><b>Parameters</b>: <code>Extends d As Date, RightDate As Date</code></li>
<li><b>Return Type</b>: <code>Dictionary</code></li>
</ul>
<p>Next, we will add some text constants to the Module, that will be used by the Method and help any calling code that wants to access the result.</p>
<ul>
<li>kDay As String = Day</li>
<li>kDay As String = Month</li>
<li>kYear As String = Year</li>
</ul>
<p>Afterwards, select the <code>Difference</code> method and type the following lines of code in the resulting Code Editor:</p>
<pre>Dim difference As Double = RightDate.TotalSeconds - d.TotalSeconds
Dim days As Integer
Dim years As Integer
Dim months As Integer
Dim dateResult As New date
dateResult.TotalSeconds = difference
days = dateResult.Day - 1
years = dateResult.Year - 1904
months = dateResult.Month - 1
Dim result As New Dictionary
result.Value(kDay) = days
result.Value(kYear) = years
result.Value(kMonth) = months
Return result</pre>
<p>As you can see, the code is simple enough. In order to simplify the code to a maximum, this example opts to give the result as an Instance of Dictionary and using the previously definied constant to store the &#8220;Year&#8221;, &#8220;Months&#8221; and &#8220;Days&#8221; of difference between the compared dates. If you want, you can use an additional Structure or Class to return the result (your own DateInterval?).</p>
<p>To test the new Class Extension, add an Open Event to the Window or App object of your project and type this lines of code:</p>
<pre>Dim d As New date(2017,10,24)
Dim d1 As New date(2017,11,30)
Dim interval As Dictionary = d.Difference(d1)
MsgBox interval.Value(kYear) + " " + interval.Value(kMonth) + " " + interval.value(kDay)</pre>
<h2>A new Date Class that makes the Difference</h2>
<p>There is a third option that we can explore that is more OOP oriented: create our own Date class and implement the subtract operator on it. More or less the thing that we do when using Xojo.Core.Date.</p>
<p>For that, the first step is adding a new class to a Xojo Project (Insert &gt; Class). Use the following data in the Inspector Panel:</p>
<ul>
<li><strong>Name:</strong> MyDateClass</li>
<li><strong>Super:</strong> Date</li>
</ul>
<p>Next, add to the class the same constant we defined in the previous section (kDay, kMonth and kYear). These will be used to define the keys in the resulting dictionary and to ease operations from the calling code too.</p>
<p>The most important thing is adding a new Method to our class with the following data:</p>
<ul>
<li><strong>Method Name:</strong> Operator_Subtract</li>
<li><strong>Parameters:</strong> rightMostDate as Date</li>
<li><strong>Return Type:</strong> Dictionary</li>
</ul>
<p>The <strong>Operator_Subtract</strong> method instructs the Xojo compiler so you can use the minus sign (-) between two instances of the class. Type these lines of code for the method:</p>
<pre>Dim difference As Double = self.TotalSeconds - rightMostDate.TotalSeconds

Dim days As Integer
Dim years As Integer
Dim months As Integer

Dim dateResult As New date

dateResult.TotalSeconds = difference

days = dateResult.Day - 1
years = dateResult.Year - 1904
months = dateResult.Month - 1

Dim result As New Dictionary

result.Value(kDay) = days
result.Value(kYear) = years
result.Value(kMonth) = months

Return result</pre>
<p>Then in the Open Event of a Window, or the App object, check the class with the following code:</p>
<pre>Dim d2 As New MyDateClass(2017,10,24)
Dim d3 As New MyDateClass(2017,11,30)

interval = d3 - d2

MsgBox interval.Value(kYear) + " " + interval.Value(kMonth) + " " + interval.value(kDay)</pre>
<p><em>Javier Rodri­guez has been the Xojo Spanish Evangelist since 2008, he’s also a Developer, Consultant and Trainer who has be using Xojo since 1998. He manages <a href="http://www.aprendexojo.com">AprendeXojo.com</a> and is the developer behind the GuancheMOS plug-in for Xojo Developers and the Snippery app, among others.</em></p>
<p>*<a href="https://www.aprendexojo.com/2017/10/truco-restar-fechas-en-xojo/">Read this post in Spanish</a></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>The Xojo.Core.Date Class</title>
		<link>https://blog.xojo.com/2014/12/11/the-xojo-core-date-class/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Thu, 11 Dec 2014 00:00:00 +0000</pubDate>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Date Class]]></category>
		<category><![CDATA[Xojo Framework]]></category>
		<guid isPermaLink="false">http://blogtemp.xojo.com/2014/12/11/the-xojo-core-date-class/</guid>

					<description><![CDATA[The Xojo.Core.Date Class]]></description>
										<content:encoded><![CDATA[<p>Ready to learn a bit about the Date class in the new Xojo framework?</p>
<p><span id="more-170"></span></p>
<p><span style="line-height: 1.62;">First, remember the old Date class remains available for desktop/web/console projects.</span></p>
<p>In this post I&#8217;m specifically talking abou the new <a href="http://xojo.helpdocsonline.com/date">Xojo.Core.Date</a> class. The big change is that Xojo.Core.Date is now <strong>immutable</strong>. With the old Date class, if you changed Date properties in the &#8220;wrong&#8221; order, such as set Day to 31 before setting Month to 9 then unexpected things start to happen. It also caused problems with things like FolderItem.ModificationDate (for example) because you might think you could actually change the modification date by directly changing its Date properties, but that doesn&#8217;t actually work.</p>
<p>So Xojo.Core.Date is immutable. You get a new Date by using one of the Constructors. If you need the current date, you use <strong>Date.Now</strong>. And with this we&#8217;ve added the companion <a href="http://xojo.helpdocsonline.com/dateinterval">DateInterval</a> and <a href="http://xojo.helpdocsonline.com/timezone">TimeZone</a> classes to make it easy to do math on Dates and to convert a date/time to a different time zone.</p>
<p>In particular, you can now do arithmetic on Dates. For example, if you want to get the Date that is two months earlier than now, you can create a DateInterval and subtract it from the Date:</p>
<pre>Dim twoMonths As New DateInterval
twoMonths.Month = 2 // 2 month interval

// Get date two months before today
Dim past As Date = Date.Now - twoMonths</pre>
<p>Or if you want to know the the interval until Jan 1, 2030:</p>
<pre>Dim d2 As New Date(2030, 1, 1, TimeZone.Current)
Dim interval As DateIntervalinterval = d2 - Date.Now</pre>
<p>Need to convert the current date/time to GMT? Try this:</p>
<pre>Dim GMTZone As New TimeZone("GMT")
Dim GMTDate As New Date(Date.Now.SecondsFrom1970, GMTZone)</pre>
<p><span style="line-height: 1.62;">You can use <a href="http://xojo.helpdocsonline.com/date">Xojo.Core.Date</a> in all project types (Desktop, Web, Console and iOS).</span></p>
<p>Update (June 2020):</p>
<p>Since this was written, the <a href="https://documentation.xojo.com/api/deprecated/date.htmlTime">DateTime</a> class was added as part of API 2.0.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
