Prior Week first day and last day to implement in Cognos - cognos-10

I have two date prompts FromDate and Todate in Cognos. FromDate should always be Prior Week first Day (i.e, Monday) and ToDate should be last day of that week(i.e., Sunday). Any help will be appreciated. thanks

FromDate
The following expression will return Monday of the previous week:
_add_days(current_date, (-1 * _day_of_week(current_date,2)) - 7)
ToDate
From there you can get the Sunday following the FromDate value:
_add_days([FromDate],6)
The only issue is what range you want when the report is run on Monday. Using the above expressions the report will go back an extra week when run on Monday. To get around this you can test for Monday and change the FromDate expression accordingly:
CASE _day_of_week(current_date,1)
WHEN 1 THEN _add_days(current_date, -1 * _day_of_week(current_date,2))
ELSE _add_days(current_date, (-1 * _day_of_week(current_date,2)) - 7)
END
When run on a Monday using this expression the previous Monday FromDate will be the previous Monday and ToDate will be the Sunday just before the current date.

The only way to dynamically set prompt defaults for date prompts is to use JavaScript. Thankfully, Cognos has provided a fully-supported JavaScript prompt API in versions 10.2.1 and above.
In order to reference prompt objects via the Cognos JavaScript API you have to give the prompt a unique name. For the following code I've assumed your "from date" prompt has been named "FromDate" and the "to date" prompt has been named "ToDate".
Insert the following code into a new HTML object. Make sure the HTML object appears after any prompt objects.
<script>
var report = cognos.Report.getReport('_THIS_');
var FromDate = report.prompt.getControlByName('FromDate');
var ToDate = report.prompt.getControlByName('ToDate');
var today = new Date();
var fromdate = new Date();
var todate = new Date();
fromdate.setDate(today.getDate() - (today.getDay() + 6));
todate.setDate(today.getDate() - today.getDay());
var fromdatestring = fromdate.getFullYear() + '-' + ("0" + (fromdate.getMonth() + 1)).slice(-2) + '-' + ("0" + fromdate.getDate()).slice(-2);
var todatestring = todate.getFullYear() + '-' + ("0" + (todate.getMonth() + 1)).slice(-2) + '-' + ("0" + todate.getDate()).slice(-2);
FromDate.addValues([{'use':fromdatestring}]);
ToDate.addValues([{'use':todatestring}]);
</script>

Related

Add Days to Date with 'YYYYMMDD' format

In VB6, I am trying to add days to a date which is in the format 'YYYYMMDD'. I can add days like this:
Pull_Date = Need_Date + Val(txtLeadTime.Text)
which works, until the resulting days is greater than the number of days in the month. I tried using DateAdd, but it doesn't accept the YYYYMMDD format - neither does CDate.
You need to convert your date string to a date so you can use the DateAdd function:
Dim Need_Date As String
Dim Pull_Date As Date
Dim tmpDate As Date
Need_Date = "20141113"
tmpDate = CDate(Mid$(Need_Date, 5, 2) & "/" & Right$(Need_Date, 2) & "/" & Left$(Need_Date, 4))
Pull_Date = DateAdd("d", Val(txtLeadTime.Text), tmpDate)
MsgBox Format$(Pull_Date, "yyyymmdd") '// if LeadTime is 25 days, displays 20141208
Please note, parsing your Need_Date by character position can blow up if it's not formatted exactly as expected.
After digging deeper into this old code, I found that a conversion function was already created:
Public Function Date_Format_YYYYMMDD(ByVal sDate As String) As String
If IsDate(sDate) Then
sDate = Format(sDate, "YYYYMMDD")
End If
Date_Format_YYYYMMDD = sDate
End Function
With this function, it's really easy:
Pull_Date = Date_Format_YYYYMMDD(Need_Date) + Val(txtLeadTime.Text)
The only thing I don't understand is why
Pull_Date = Format(Need_Date, "YYYYMMDD") + Val(txtLeadTime.Text)
doesn't work. Seems like it's doing the same thing as the function.

Get next week in second textbox

I have 2 textboxes.
I want that when I pick date in first textbox using datepicker then second TextBox automatically show next week date.
How can I do this ?
Here is my code-
Use DateTime.AddDays:
txtToDate.Text = DateTime.Parse(txtDateFrom.Text, System.Globalization.CultureInfo.InvariantCulture).AddDays(7).ToString("dd.MM.yy");
You can use this code in txtDateFrom leave event or value change event
TextBox2.Text = Convert.ToDateTime(TextBox1.Text).AddDays(7.0).ToString();
You need to make correct datetime format like (MM/dd/yyyy) of textbox before convert. If you use dd/MM/yyyy format then check below code.
string[] date1 = TextBox1.Value.Split('/');
string FinalDate1 = date1[1] + "/" + date1[0] + "/" + date1[2];
TextBox2.Text = Convert.ToDateTime(FinalDate1).AddDays(7.0).ToString("dd/MM/yyyy");
Add 7 days to the first date value.
DateTime date1 = System.DateTime.Now;
DateTime date2 = date1.AddDays(7);

ASP Classic Get Next Date for a WeekDayName

I would like to specify a number that specifies the day of week and then have ASP get the upcoming date for that week day specified.
Example:
Dim xWeekDay
xWeekDay=1 ' <-- 1 would be a Monday...and Sunday would be 7
Dim NextDdate
NextDdate= ???? <-- I want to calculate and show the Upcoming Date here
So the above line would look like this when it's populated.
NextDdate=7/1/2013
Try this:
today = Weekday(Date, vbMonday)
If xWeekDay > today Then
NextDate = Date + (xWeekDay - today)
Else
NextDate = Date + (xWeekDay + 7 - today)
End If
Weekday(Date, vbMonday) is the number of the currend day of the week (with Monday being set as the first weekday). If xWeekDay is in the future (xWeekDay > today), then the next occurrence is xWeekDay - today days away. Otherwise it's xWeekDay + 7 - today days away. Add that difference to the current date and you have the date you're looking for.

Given a date how to get Sunday & Saturday of that week

I want to get the Sunday & Saturday of the week from which a date is provided.
I have access to the following functions only:
getDate() returns a number from 0-6 (0 being sunday)
getDay() returns a number from 1-31
getMonth() returns a number from 0-11
getFullYear() returns the current year
I am doing this on titanium.
Per your description above, I came up with:
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDate() + getDay());
var sun = new Date(input.getFullYear(), input.getMonth(), getDay() + (input.getDate() - 6));
If I follow the MDN doc, I come up with (works in Ti too):
var sat = new Date(input.getFullYear(), input.getMonth(), 6 - input.getDay() + input.getDate());
var sun = new Date(input.getFullYear(), input.getMonth(), input.getDate() + (input.getDay() - 6));
Where input is a javascript Date object.
The date object will take care or changing the month and/or year if necessary.
Hope this helps.

retrievind date in asp.net

I need to retrieve the current date in asp.net and then compare that with the date given by the user in textbox1.text(mm/dd/yyyy format), if date date given is greater than current date then error else add 4months2days with that date and display it in textbox2.text.
help me please,
thanking you guys,
Indranil
DateTime dateToCompare;
if(DateTime.TryParse(textbox1.text, out dateToCompare))
{
DateTime current = DateTime.Now;
TimeSpan ts = current - dateToCompare;
if (ts.Ticks < 0)
{
//display error
}
else
textbox2.text = dateToCompare.AddMonths(4).AddDays(2).ToString("mm/dd/yyyy");
}
}
I'm not going to write your code, but in .NET you can use ToString to specify a date format, TryParse to get a date out of a string. And AddDays, AddMonths etc to manipulate a date.
In javascript, there's no simple way to format output, but you can use getMonth etc to prompt the individual values and concatenate a string from that. You can use a combination of getDate and setDate to manipulate dates. It automatically corrects for new months, i.e. if you run myDate.setDate( myDate.getDate() + 60 ) it'll actually increment by 60 days; you won't end up with a weird date like May 74th.
Keep in mind that months in javascript are zero-based, ie January is 0, February is 1, etc.
You can create a new date in javascript by new Date(yy, mm, dd) or new Date('yy/mm/dd'), so you could string-manipulate an input and create a date from that.
To compare two dates, you can subtract one from the other, and get the difference in milliseconds.
if ( dateA - dateB < 0 ) // dateB is greater than dateA (occurrs later)
and
var diff = Math.abs(dateA - dateB) // difference in ms, no matter which date is greater
DateTime date1 = new DateTime();
if(DateTime.TryParse(textbox1.text, out date1)){
if (date1.CompareTo(DateTime.Now) > 0)
{
//Error code here
}else
{
textbox2.text = date1.AddMonths(4).AddDays(2);
}
}

Resources