Lasso Soft Inc. > Home

[date_firstdayofweek]

Linkdate_firstdayofweek
AuthorJason Huck
CategoryDate
Version8.x
LicensePublic Domain
Posted20 Jul 2006
Updated02 Aug 2007
More by this author...

Description

Returns the date of the first day of the week for the given date. If no date is provided, it uses today's date. Optionally accepts a -starton parameter to indicate whether the week should start on Sunday (1) or Monday (2).

Sample Usage

date_firstdayofweek;

date_firstdayofweek( -date=date('7/20/2006'), -starton=2);

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define_tag(
	'firstdayofweek',
	-namespace='date_',
	-opt='date', -type='date', -copy,
	-opt='starton', -type='integer',
	-priority='replace',
	-description='Returns the date of the first day of the week for the given date.'
);
	!local_defined('date') ? local('date') = date;
	#date->set( -hour=14, -minute=0, -second=0);
	
	!local_defined('starton') ? local('starton') = 1;
	(: 1, 2) !>> #starton ? #starton = 1;
	
	local('daynum') = integer(#date->format('%w'));
	
	if(#daynum == #starton);
		return(#date);
	else(#daynum < #starton);
		return(#date->add( -day=1)&);
	else;
		return(#date->subtract( -day=(#daynum - #starton))&);
	/if;
/define_tag;

Related Tags

Comments

06 Feb 2009, Stefan Straakenbroek

Update

When you have the date which starts on a sunday (03/01/2009) and you set the starton on monday (2). The returned day will be (03/02/2009) instead of (02/23/2009).
Extend the if/else as below to fix this problem.

if(#daynum == #starton);
return(#date);
else(#daynum < #starton && #starton == 2 && #daynum == 1);
return(#date->subtract( -day=6)&);
else(#daynum < #starton);
return(#date->add( -day=1)&);
else;
return(#date->subtract( -day=(#daynum - #starton))&);
/if;

02 Aug 2007, Jason Huck

Update

The tag now passes the source date by copy instead of reference so as not to inadvertently change the source date!

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft