i am working in .Net Charts. i want to show date in X-Axis. For ex : if i select Last 52 Weeks, then i should show the chart for last 52 weeks, whereas those 52 weeks start date should be in x-axis. I am not having any idea, how to do this..I had tried with the code..
DateTime Frm = sessionManager.ChartViewPeriodFrom;
DateTime To = sessionManager.ChartViewPeriodTo;
double min = Frm.ToOADate();
double max = To.ToOADate();
Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min;
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max;
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 7;
Here i am getting the Frm as "9/17/2011 12:00:00 AM" But, in the chart the minimum date starts from "9/21/2011 12:00:00 AM". How to fix this...
I had tried like this also..[ Edited Part ]
Chart1.Series["Series1"].XValueType = ChartValueType.Date;
Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = (new DateTime(2011, 09, 17, 12, 00, 00)).ToOADate();
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = (new DateTime(2012, 09, 08, 12, 00, 00)).ToOADate();
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 7;
Chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
After a long search...i had fixed my above issue...but still i dont know how it works...
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
Chart1.Series["Series1"].XValueType = ChartValueType.Date;
DayOfWeek ds = DayOfWeek.Wednesday;
double dblIntervalOffset = Convert.ToDouble(ds);
Chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = dblIntervalOffset;
Chart1.ChartAreas["ChartArea1"].AxisX.Minimum = min;
Chart1.ChartAreas["ChartArea1"].AxisX.Maximum = max;
Chart1.ChartAreas["ChartArea1"].AxisX.Interval = 7;
Chart1.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
Indexed attribute must be set for series data.
Related
I am trying to develop a code to generate number of working days in each month between two selected dates:
Eg: Start Date is 20-Oct-2022 and End Date is 14-Feb-2023.
I am able to generate net working days between two dates, however not for each month between the two dates.
I am expecting the code to provide output as:
Net working days in
Oct'22 is 8,
Nov'22 is 21,
Jan'23 is 22 and
Feb'22 is 10.
var startDate = new Date('20/10/2022');
var endDate = new Date('14/02/2023');
var numOfDates = getBusinessDatesCount(startDate,endDate);
function getBusinessDatesCount(startDate, endDate) {
let count = 0;
const curDate = new Date(startDate.getTime());
while (curDate <= endDate) {
const dayOfWeek = curDate.getDay();
if(dayOfWeek !== 0 && dayOfWeek !== 6) count++;
curDate.setDate(curDate.getDate() + 1);
}
return count;
}
like in above example you can calculate number of working days between two dates.
I am getting Date as '2/12/2020 4:30:29 PM'. But i need trimmed year and seconds date time format like
'12/02/20 04:30 PM'
what is the equivalent function in MEL for getting above date time?
Thanks
I'm afraid there isn't an equivalent function in MEL to do this thing. BUT the good news is that you can create it! (yes, MEL needs more time functions to work...).
I've create this global function that you can use it (I assume 2 is day and 12 is month, if not you can change the order):
separator = "/";
space = " ";
hourSeparator = ":";
$global:stringDatetimeToArray = function(datetime)
{
array['year'] = substring(datetime, 6, 10);
array['month'] = substring(datetime, 3, 5);
array['day'] = substring(datetime, 0, 2);
array['hour'] = substring(datetime, 11, 13);
array['minute'] = substring(datetime, 14, 16);
array['seconds'] = substring(datetime, 17, 19);
array['meridian'] = substring(datetime, 20, 22);
return array;
};
concat(array['month'], separator, array['day'], separator, array['year'], space, array['hour'], hourSeparator, array[minute], space, array['meridian']);
My recommendation for you is to generate a method that converts a timestamp into an array of these values, and then you can work with these kind of issues easier than now. You can see an example on this github script
import re
date_str = '2/12/2020 4:30:29 PM'
sub_str = re.search(':\d+(.*?)\s',date_str).group(1)
date_str = date_str.replace(sub_str,'')
print(date_str)
Output: 2/12/2020 4:30 PM
together
how do I get it in PowerPivot or as Dax formula out that for my internal / individual months the beginning of the month from 31.120.2018 for January 2019 receive?
So, that I get the January in the PowerPivot calendar table already from 31.12.2018 and then in the 4-4-5 weeks cycle?
So
31.12.2018 + 28 days = January
31.12.2018 + (28 days * 2) = February
31.12.2018 + (28 days * 2) + 7 days = March
Does anyone have an idea?
Best Regards
First I create a calendar table, this is the simple part, I took a random start and end for testing:
CalendarDates = CALENDAR(DATE(2012;12;20);DATE(2021;1;5))
Then I added the column WeekNumber, this is to the greorgian calendar. A bit complicated because microsoft simply starts counting with 1 on the first of january..
WeekNumber =
var wNrfirstDayJanNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;1);2)
var fullWeekNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;7);2) = 1 //If day 7 of week is still first week, its a full week
var wNr31Dex = WEEKNUM(DATE(CalendarDates[Date].[Year];12;31);2)
var wNr31DexPrevYear = WEEKNUM(DATE(CalendarDates[Date].[Year]-1;12;31);2)
var wNr = WEEKNUM(CalendarDates[Date];2)
var pbi4JanWeekNrNextYear = WEEKNUM(DATE(CalendarDates[Date].[Year] + 1;1;4);2)
var pbi4JanWeekNr = WEEKNUM(DATE(CalendarDates[Date].[Year];1;4);2)
var wNrComp = if (pbi4JanWeekNr =2; wNr - 1; wNr)
return if(NOT(fullWeekNextYear) && wNr = wNr31Dex;IF(pbi4JanWeekNrNextYear = 2; wNr31Dex;1);if(wNrComp = 0; wNr31DexPrevYear; wNrComp))
last step is to create the Month based on the week number:
Month =
var quater = FLOOR((CalendarDates[WeekNumber] - 1)/13; 1)
var quarterWeek = MOD(CalendarDates[WeekNumber] - 1 - (quater*13);13)
var monthNr = FLOOR(quarterWeek/4;1)
var month445 = if (monthNr = 3; 2; monthNr) + quater * 3 + 1
return if (month445 = 13; 12;month445)
Note: I corrected week 53 in the last quarter so it becomes 4-4-6. You need to do your own math if you want it different.
End result:
I am having a little issue with a date generator code below. The list of the code below is to ensure that a random date is selected within the summer months (May, June, July August) for availability.
So what I did is that I say if the current month is less than 5 (less than May), then select a random date between 1st May this year till 31st August this year, else if the current month is past 7 (past July), then select a random date between 1st May next year till 31st August next year.
Now I notice a little glitch in my code I require help with. As I ran the code below today (8th May), it is possible that the date the random generator selects could be in May before today's date. Actually the issue is I don't have anything to handle when I am in the current months. So I think I require a little refactoring.
What i would like is that it checks the current date and if it between May to July (so not less than May or more than July), then check today's date and pick a date between today till the 31st August this year.
My brain has been fried and for some strange reason I am struggling on something which logically makes sense, but I've just been having issues programming it.
import groovy.time.TimeCategory
//def dataSet = testRunner.testCase.getTestStepByName("Properties")
// Select the current test data line
def dateFormat = 'yyyy-MM-dd'
def getNumberInRange = { min, max -> new Random().nextInt(max + 1 - min) + min }
def isTodayBeforeMay = { Calendar.MONTH < 5 }
def isTodayAfterJuly = { Calendar.MONTH > 7 }
//Get the number of days between today and given date
def getDifferenceDays = { targetDate, closure ->
def strDate = closure (targetDate)
def futureDate = new Date().parse(dateFormat, strDate)
TimeCategory.minus(futureDate, new Date()).days
}
//Get the offset between today and max date i.e.,31 august
def getOffSetDays = { date ->
//Need to change the date range if needed.
//As per OP, May to August is mentioned below
def max = getDifferenceDays(date) { "${it[Calendar.YEAR]}-08-31" }
def min = getDifferenceDays(date) { "${it[Calendar.YEAR]}-05-01" }
getNumberInRange(min, max)
}
def now = new Date()
def nextYearNow = now.updated(year: now[Calendar.YEAR] + 1)
def selected
def finalDate
log.info "Today : $now"
log.info "Next year same date : $nextYearNow"
if (isTodayBeforeMay()) {
selected = now
} else if (isTodayAfterJuly()) {
selected = nextYearNow
} else {
throw new Error("Not implemented for the days between 1st May to 30th July")
}
def offset = getOffSetDays(selected)
//Add the offset days to now
use(TimeCategory) {
finalDate = now + offset.days
}
All you need is to implement the else condition instead of throw new Error(..) below (code excerpt from the question):
If you read the code, it is crystal clear each condition and error message as place holder for the unknown data range in below and which is now you wanted it to be handled.
if (isTodayBeforeMay()) {
selected = now
} else if (isTodayAfterJuly()) {
selected = nextYearNow
} else {
throw new Error("Not implemented for the days between 1st May to 30th July")
}
Just add the below statement in the last else in place of threw new Error
selected = getOffSetDays(now)
EDIT:
You can try quickly online Demo
EDIT 2: Looks the above is not working at times randomly, so updating the answer:
import groovy.time.TimeCategory
def dateFormat = 'yyyy-MM-dd'
def getNumberInRange = { max, min = 1 -> new Random().nextInt(max + 1 - min) + min }
def isTodayBeforeMay = { Calendar.MONTH < 5 }
def isTodayAfterJune = { Calendar.MONTH > 6 }
//Get the number of days between today and given date
def getDifferenceDays = { targetDate, closure ->
def strDate = closure (targetDate)
def futureDate = new Date().parse(dateFormat, strDate)
TimeCategory.minus(futureDate, new Date()).days
}
def getPaddedString = { num, len = 2, padwith = '0' ->
num.toString().padLeft(len, padwith)
}
//Get the offset between today and max date i.e.,31 august
def getOffSetDays = { date, minMonth = 5, minDay = 1 ->
//Need to change the date range if needed.
//As per OP, May to August is mentioned below
def max = getDifferenceDays(date) { "${it[Calendar.YEAR]}-08-31" }
def min = getDifferenceDays(date) { "${it[Calendar.YEAR]}-${getPaddedString(minMonth)}-${getPaddedString(minDay)}" }
getNumberInRange(max, min)
}
def now = new Date()
def nextYearNow = now.updated(year: now[Calendar.YEAR] + 1)
def selected
def finalDate
println "Today : $now"
println "Next year same date : $nextYearNow"
if (isTodayBeforeMay()) {
selected = now
} else if (isTodayAfterJune()) {
selected = nextYearNow
}
def dayz = getNumberInRange(getDifferenceDays(now) { "${it[Calendar.YEAR]}-08-31" })
def offset = selected ? getOffSetDays(selected) : dayz
offset = offset > 0 ? offset : now[Calendar.DAY_OF_MONTH]+1
//Add the offset days to now
use(TimeCategory) {
finalDate = now + offset.days
}
println "Final future date is : $finalDate"
println "Final future date is(formatted) : ${finalDate.format(dateFormat)}"
assert now <= finalDate
This Demo generate the date 1000 times just to make sure the date is ok.
I was recently trying to determine the answer to this question. The only post I was able to find on the topic was this old unanswered post on Flexera's website.
I wanted to know the answer to this question to incorporate in a tool for managing string translations. I already discovered the answer (my coworker and I spent the better half of our day trying to figure it out) but I thought I'd post the question/answer on Stack Overflow just in case someone else searches for it.
The answer is that the timestamp is a 32-bit integer with different bits representing different parts of the date.
Here's how it breaks down
Bits 1-5 : The Day of the Month [1-31] (end range could be 28-31 depending on month)
Bits 6-9 : The Month [1-12]
Bits 10-16: The Year after 1980 (only goes to year 2107) [0-127]
Bits 17-21: (?) Seconds rounded to even (only 5 bits so can only contain 31 values) [0-30]
Bits 22-27: Minutes [0-59]
Bits 28-32: Hours from 12 AM [0-23]
If the 32-bit integer is an invalid date it's evaluated to a default date Dec/30/1899 12:00 AM
Here is an example:
-------BINARY-32-bit-Integer----- | Decimal | Date String
DOM Month Year Seconds*2 Min Hour | |
00111 0111 0010000 00001 010000 00000 | 999295488 | Jul/07/1996 12:16 AM
7 7 16 1 16 0
Here is some C# code written to convert between DateTime and the string representation of the ISString timestamp (Small Disclaimer: this code doesn't currently handle invalid timestamp input):
private static int bitsPerDOM = 5;
private static int bitsPerMonth = 4;
private static int bitsPerYear = 7;
private static int bitsPerEvenSecond = 5;
private static int bitsPerMinute = 6;
private static int bitsPerHour = 5;
private static int startYear = 1980;
public static string getISTimestamp(DateTime date)
{
int[] shiftValues = { bitsPerDOM, bitsPerMonth, bitsPerYear, bitsPerEvenSecond, bitsPerMinute, bitsPerHour };
int[] dateValues = { date.Day, date.Month, date.Year -startYear, date.Second/2, date.Minute, date.Hour };
int shift = 32;
int dateInt = 0;
for (int i = 0; i < dateValues.Length; i++)
{
shift -= shiftValues[i];
dateInt |= (dateValues[i] << shift);
}
return dateInt.ToString();
}
public static DateTime getTimeFromISTimestampStr(string ISTimestampStr)
{
int timestampInt = Int32.Parse(ISTimestampStr);
int dom = getBits(timestampInt, 0, 4);
int month = getBits(timestampInt, 5, 8);
int year = startYear + getBits(timestampInt, 9, 15);
int seconds = getBits(timestampInt, 16, 20) * 2;
int minutes = getBits(timestampInt, 21, 26);
int hours = getBits(timestampInt, 27, 31);
return new DateTime(year, month, dom, hours, minutes, seconds);
}
private static int getBits(int n, int start, int end)
{
//Clear left bits by shifting
n <<= start;
n >>= 31 + start - end; //Shift to the right
return n;
}