I need to calculate the age of a person taking in consideration the years, months and days to check if the person is under 14 or not.The return result should be boolean if the person is under 14 or not. I tried with this code but I'm getting an error of conversion.          var under14 = false;         string person.DateOfBirth = "04/01/2012";         var DateOfBirth = Convert.ToInt32(person.DateOfBirth);         var ageDifMs = Convert.ToInt32(DateTime.Now) - DateOfBirth;         var ageDate = new DateTime(ageDifMs);         var age = Math.Abs(ageDate.Year - 1970);         if (age &lt; 14)             under14 = true;
1660
Given a DateTime representing a person's birthday, how do I calculate their age in years?
88507
I have a string as,  field name : start Date: 2016-11-14 08:00 AM ( string type)  how to convert this to Date time. I tried Date.parseDate and below option also.  String val = recruiters[j].Start_Time_Formula__c;                              system.debug('val ::'+ val);                             DateTime date1 = (DateTime)Json.deserialize('"'+val+'"', DateTime.class);                             system.debug('date1 ::' + date1);   but I am getting error as,     System.JSONException: Invalid format: "2016-11-14 08:00 AM" is malformed at " 08:00 AM": (System Code)   I referred the below link and got with the above error.    any help ?
57062
Today I needed a simple algorithm for checking if a number is a power of 2.  The algorithm needs to be:   Simple Correct for any ulong value.   I came up with this simple algorithm:  private bool IsPowerOfTwo(ulong number) {     if (number == 0)         return false;      for (ulong power = 1; power &gt; 0; power = power &lt;&lt; 1)     {         // This for loop used shifting for powers of 2, meaning         // that the value will become 0 after the last shift         // (from binary 1000...0000 to 0000...0000) then, the 'for'         // loop will break out.          if (power == number)             return true;         if (power &gt; number)             return false;     }     return false; }   But then I thought, how about checking if log2 x is an exactly round number? But when I checked for 2^63+1, Math.Log returned exactly 63 because of rounding. So I checked if 2 to the power 63 is equal to the original number - and it is, because the calculation is done in doubles and not in exact numbers:  private bool IsPowerOfTwo_2(ulong number) {     double log = Math.Log(number, 2);     double pow = Math.Pow(2, Math.Round(log));     return pow == number; }   This returned true for the given wrong value: 9223372036854775809.  Is there a better algorithm?
70243
I'm trying to adjust this code so that I can jump to today's date which are all across row 2 in my spreadsheet.  function onOpen() {  var ss = SpreadsheetApp.getActiveSpreadsheet();  var sheet = ss.getActiveSheet();  var range = sheet.getRange("A:A");  var values = range.getValues();    var day = 24*3600*1000;    var today = parseInt((new Date().setHours(0,0,0,0))/day);    var ssdate;   for (var i=0; i&lt;values.length; i++) {    try {      ssdate = values[i][0].getTime()/day;    }    catch(e) {    }    if (ssdate &amp;&amp; Math.floor(ssdate) == today) {      sheet.setActiveRange(range.offset(i,0,1,1));      break;    }      } }    is from Marshmallow  I tried changing sheet.getRange("A:A") to sheet.getRange("2:2") and sheet.getRange("C2:PB2") but neither work.
96393
I need to determine the number of days in a month for a given date in SQL Server.  Is there a built-in function? If not, what should I use as the user-defined function?
20073
I've been using this function but I'd like to know what's the most efficient and accurate way to get it.  function daysInMonth(iMonth, iYear) {    return 32 - new Date(iYear, iMonth, 32).getDate(); }
102384
I am still not able to get the correct difference, really struggling to make it working. The business hour set from 10 AM to 7 PM  BusinessHours bh = [SELECT FridayEndTime,FridayStartTime,Id,MondayEndTime,MondayStartTime,                     FROM BusinessHours where Name='Test Business Hour'                     LIMIT 1]; Time startTime = bh.MondayStartTime;        Time endTime = bh.MondayEndTime;  Date d = Date.today(); Datetime monTodaystart = Datetime.newInstance(d.year(),d.month(),d.day(),startTime.hour(), startTime.minute(),startTime.second()); Datetime monTodayend = Datetime.newInstance(d.year(),d.month(),d.day(),endTime.hour(), endTime.minute(),endTime.second()); System.debug('monTodaystart :: '+monTodaystart);     System.debug('monTodayend :: '+monTodayend);   Long diff = BusinessHours.diff(bh.Id, monTodaystart, monTodayend); System.debug('diff  :: '+diff);   I tried this simple example, but still its not working why ? How we can manipulate it ?  Datetime monTodaystart = Datetime.newInstance(2017,8,20,10,0,0); Datetime monTodayEnd = Datetime.newInstance(2017,8,20,17,0,0); System.debug('ID : '+bh.Id);  Long diff = BusinessHours.diff(bh.Id, monTodaystart,monTodayEnd); System.debug('diff  :: '+diff);   I dont think I should used the API here, instead I should use my custom logic, looks like some bug in the API still exists.
71224
I have this function...  private string dateConvert(string datDate) {         System.Globalization.CultureInfo cultEnGb = new System.Globalization.CultureInfo("en-GB");         System.Globalization.CultureInfo cultEnUs = new System.Globalization.CultureInfo("en-US");          DateTime dtGb = Convert.ToDateTime(datDate, cultEnGb.DateTimeFormat);         datDate = dtGb.ToString(cultEnUs.DateTimeFormat.ShortDatePattern);          return datDate; }   But I want it with the leading zero still on lower digits (1-9) so the date is 11-09-2009 (mm-dd-yyyy)...  Now If I wasn't converting it id use string.Format("{0:d}", dateVar) how do I do this in the conversion?  ***** Solution ***** Used a slightly modified version of the answer below (i.e. one that would render).  Convert.ToDateTime(datDate).ToString("MM-dd-yyyy");
82186
I would like to filter a datetime field but I only want to filter MONTH  var requestUri = "xxxx/getByTitle('Contact')/items?$select=CellPhone,mdate,ddate&amp;$filter= Birthday ge '" + today + "'";   The query is working fine and I also tried month(birthday) as it was in the odata specifications but didn't work.  How can I get the month from the Birthday Datetime field.I can get it from my today variable.
23679
I want to calculate the age of a person given the date of birth and the current date in years, months and days relative to the current date.  For example:  &gt;&gt;&gt; calculate_age(2008, 01, 01) 1 years, 0 months, 16 days   Any pointer to an algorithm that does that will be appreciated.
63628
Here's an example:  Double d = (1/3); System.out.println(d);   This returns 0, not 0.33333... as it should.  Does anyone know?
116559
I need to decrement a Javascript date by 1 day, so that it rolls back across months/years correctly.  That is, if I have a date of 'Today', I want to get the date for 'Yesterday'.  It always seems to take more code than necessary when I do this, so I'm wondering if there's any simpler way.    What's the simplest way of doing this?  [Edit: Just to avoid confusion in an answer below, this is a JavaScript question, not a Java one.]
110786
Suppose you want to estimate the average age of all Boeing 737-300 airplanes now in active domestic U.S. service. You want to be 95% confident, and you want your estimate to be within one year of the actual figure. The 737-300 was first placed in service about 24 years ago, but you believe that no active 737-300s in the U.S. domestic fleet are more than 20 years old. How large of a sample should you take?  Solution says that E = 1 year, the z value for 95% is 1.96, and s is unknown, so it must be estimated by using s approximately equals to (1/4) * (range). As the range of ages is 0 to 20 years, s = (1/4)(20) = 5  Why do we use 1/4 * range for standard deviation ?
15186
I am working on a project where the requirement is to have a date calculated as being the last Friday of a given month.  I think I have a solution that only uses standard Java, but I was wondering if anyone knew of anything more concise or efficient. Below is what I tested with for this year:      for (int month = 0; month &lt; 13; month++) {         GregorianCalendar d = new GregorianCalendar();         d.set(d.MONTH, month);         System.out.println("Last Week of Month in " + d.getDisplayName(d.MONTH, Calendar.LONG, Locale.ENGLISH) + ": " + d.getLeastMaximum(d.WEEK_OF_MONTH));         d.set(d.DAY_OF_WEEK, d.FRIDAY);         d.set(d.WEEK_OF_MONTH, d.getActualMaximum(d.WEEK_OF_MONTH));         while (d.get(d.MONTH) &gt; month || d.get(d.MONTH) &lt; month) {             d.add(d.WEEK_OF_MONTH, -1);         }         Date dt = d.getTime();         System.out.println("Last Friday of Last Week in  " + d.getDisplayName(d.MONTH, Calendar.LONG, Locale.ENGLISH) + ": " + dt.toString());     }
58651
Does anybody know if there is a way to evaluate a string containing boolean-logic at runtime? My logic, can only contain three operators and parenthesis (AND, OR, NOT). I can even make the string look like this: true &amp;&amp; false &amp;&amp; (true || !true)  I don't have the bandwidth to build a full-on parser... Any ideas?  PS: I saw some answers that pointed to the use of an API but I don't have access to use the Toolkit API in the environment this will get deployed.
9299
Given a start date of 1/1/2009 and an end date of 12/31/2009, how can I iterate through each date and retrieve a DateTime value using c#?  Thanks!
9895
So what I need is a limitation for the user of the funded contracts. So this is what I am trying to implement. I am trying to let the user enter dates within the year 2014, which will bring up how many funded contracts are there for each month. I ONLY want up to the last day of the previous month and the passed months within the year 2014. So for example we are in June, if the user inputs any dates that are before June he can see those months of how many funded contracts are in there - but not any contract from June.   So the user should not be able to see July until we hit August which we can see July from there. Here is my query.  I feel that my where clause for the GetDate is incorrect. Any help will be appreciated             Alter Proc spGetAdminTotalYTD             (@Begin_Date  DATETIME,              @End_Date DATETIME,             @program int=null) As      Begin Declare @year int Set @year = 2014          SELECT  d.name, a.dealer_code, b.last_name, b.city, b.state, b.phone             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 1 THEN 1 ELSE NULL END) January             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 2 THEN 1 ELSE NULL END) Feburary             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 3 THEN 1 ELSE NULL END) March             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 4 THEN 1 ELSE NULL END) April             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 5 THEN 1 ELSE NULL END) May             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 6 THEN 1 ELSE NULL END) June             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 7 THEN 1 ELSE NULL END) July             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 8 THEN 1 ELSE NULL END) August             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 9 THEN 1 ELSE NULL END) September             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 10 THEN 1 ELSE NULL END) October             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 11 THEN 1 ELSE NULL END) November             , COUNT(CASE WHEN  MONTH(c.Funded_date) = 12 THEN 1 ELSE NULL END) December,             COUNT(1) AS YTD         FROM tdealer a JOIN tContact b ON a.contact_id = b.contact_id               JOIN tContract  c ON a.dealer_id = c.dealer_id JOIN tCompany d               ON  c.company_id = d.company_id         WHERE YEAR(c.Funded_date) = @Year              AND c.Funded_date &gt;= DATEADD(MONTH, DATEDIFF(MONTH, 0,GETDATE())-4,0)              AND c.Funded_date &lt;  DATEADD(MONTH, DATEDIFF(MONTH,-1,GETDATE())-4,0)              AND c.program_id = @program              AND c.funded_date Between @Begin_Date And @End_Date              GROUP BY     d.name,     a.dealer_code,     b.last_name,     b.city,     b.state,     b.phone     END