The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 LabTalk Forum
 Date Change Function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

LabTalk user

USA
35 Posts

Posted - 10/21/2010 :  11:47:11 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 8.1 SR3
Operating System: Windows XP

I have been tasked with writing a function which accepts a date in the form JAN01, 2011 and then adds x number of months to said date. I've been trying to think of ways to do this and my brain hasn't come up with anything. If anyone has any ideas that would be great.

Thanks!

greg

USA
1379 Posts

Posted - 10/21/2010 :  6:04:28 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Days would be easy:

// BEGIN SCRIPT
string str$ = "JAN01,2011"; // Input the date
str.replace(',',' '); // Replace ','
jdn = date(%(str$),"MMMdd yyyy"); // Find the julian day number
jdn += 365; // Add 365 days
str$ = $(jdn,D0); // Get the new date as a string
ty str$; // Output the new date
// END SCRIPT

Our date( ) function cannot handle ',' so I replaced ',' with ' '.
Since the date function returns days, it's easy to add days and get the date string.

The problem is : What exactly is a month?
The answer depends on which month(s) we are talking about.
This problem is compounded by leap years.

If you literally only care about months, then this does it:
// BEGIN SCRIPT
string str$ = "JAN28,1986";
addmonths = 264;
str.replace(',',' ');
jdn = date(%(str$),"MMMdd yyyy");
day = day(jdn,2); // Get the day of the month
month = month(jdn); // Get the month
year = year(jdn); // Get the year
addyears = int(addmonths/12); // Find the number of years added
addmonths = mod(addmonths,12); // And remaining months to add
newjdn = date($(month+addmonths)/$(day)/$(year+addyears));
str$ = $(newjdn,D0);
ty str$;
// END SCRIPT
Go to Top of Page

LabTalk user

USA
35 Posts

Posted - 10/22/2010 :  6:57:03 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
That is exactly what I was looking for. I can take your first example and tailor it to exactly what I need. Thank you so much for the help.

Thanks again!
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000