I'm desperately trying to understand swift's DatePicker. I have a need to select dates from Julian Day 0 (11/24/4714 BCE) to dates in the future. When I extract the DateComponents from the DatePicker, they do not appear to be a 'proleptic' gregorian calendar. For instance, by selecting 3/1/300 AD (or CE), the month and day are 2/29. This seems consistent with the Julian Calendar, a date I cannot select with the DatePicker's calendar set to iso8601 or gregorian. I am familiar with the date of adoption of the Gregorian calendar, and indeed this is where the discrepancies begin. But why won't it calculate the proleptic dates, or am I misunderstanding this. Extracting these components is necessary for calculating various quantities such as Julian Day or converting from one calendar to another. Even a suggestion of where to find the info would be appreciated.
---Edited to show code -----
#IBAction func calculate(_ sender: Any) {
var mycal = Calendar(identifier: .iso8601)
//var mycal = Calendar(identifier: .gregorian)
let picked_date:Date = datePicker.dateValue
let formatter1 = DateFormatter()
formatter1.locale = Locale(identifier: "en_US_POSIX")
formatter1.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
let picked_format = formatter1.string(from: picked_date)
//print("picked_format: \(picked_format)")
let format_date = formatter1.date(from: picked_format)
var tz: String { return TimeZone.current.identifier }
let myzone = UTC.state == NSOnState ? tz : "GMT"
//let era = AD.state == NSOnState ? 1 : 0
mycal.timeZone = TimeZone(identifier: myzone)!
var comps = mycal.dateComponents([.year,
.month,
.day,
.hour,
.minute,
.second,
//.era,
//.nanosecond,
.timeZone],
from: format_date!)
print(comps.month!)
print(comps.day!)
Bottom line: The discrepancies in what was shown in DatePicker vs the 'elements' of the date object it produced (namely, YEAR, MONTH and DAY) were due wholly to a careless mistake I made writing the code: The failure to ensure that all the objects that dealt with dates were specified to the same Calendar. Initially, my DateFormatter and DateComponents objects were set to ".iso8601" and failing to specify a calendar for the DatePicker defaulted it to my locale, which meant ".gregorian". This produced a 10 day discrepancy in the date prior to Oct 15, 1582. Also: A DatePicker using the Gregorian calendar will allow you to select days from 5 Oct 1582 - 14 Oct 1582 (inclusive), just as I would expect from a proleptic calendar. A DatePicker using the ISO calendar will NOT allow dates in this range. Stepping down from 15 Oct 1582 by one day will result in the DatePicker showing 4 Oct 1582 ... not the result I would expect if it is based on the proleptic Gregorian.
Some caveats, though. Apple's documentation and the ISO standard itself lead me to believe that the ISO calendar was based on a proleptic Gregorian calendar. This proved to be untrue. You can repeat for yourself the following: Create a DatePicker specifying the ISO calendar and it will allow you to choose dates which do not exist in the Gregorian calendar, namely leap years for such years as 1500, 1400, 1300, 1100, 1000, etc. This is more in keeping with the ISO calendar behaving like the Julian Calendar prior to the adoption date. Indeed, the date discrepancies begin to narrow as the year number decreases because the ISO calendar is counting those years as leap years. The error becomes ZERO from 1 March 200 - 28 Feb 300 AD, exactly as one would expect of a Julian Calendar vs a proleptic Gregorian calendar. I don't know if I am misunderstanding how the ISO calendar is supposed to behave, or if this is a bug. Thanks to Willeke for leading me to the correct solution!
Related
I am using a calendar table in an slicer in my Power BI report. In order to avoid the user to select the current month, I would like this calendar to go up to the last day of the previous month. i.e., If we are in February, it would only display the dates until Jan 31.
How can I define the end date of my Calendar DAX.
CALENDAR (
DATE(2019,1,1),
TODAY()
)
I have tried a couple of things included ENDOFMONTH() and EOMONTH(), but none of them worked.
If you want to exclude, then you could calculate the end date of the previous month using EOMONTH function and then pass this date to CALENDAR, e.g. like this:
var CalendarTable =
var LastMonthEnd = EOMONTH(TODAY(),-1)
var DatesRange = CALENDAR (
DATE(2019,1,1),
LastMonthEnd
)
return DatesRange
I like Andrey's solution, but want to flagg that you can do it like this as well:
Calendar =
CALENDAR(
DATE(2019,1,1),
TODAY()-DAY(TODAY())
)
As always, in DAX there are several ways of doing the same thing.
I want to set 10 years back date from today date in kendo datepicker.
If you want to initialize your date picker with 10 years back date always, then in your HTML it would be something like this:
#(Html.Kendo().DatePicker().Name("myDatePicker").Value(DateTime.Today.AddYears(-10)))
For an example: When we are working with any Bank form applications, the applicant should have minimum 20 years from Today's Date to apply a particular Exam.
So It's Better to give Max-Date as the Day 20 years from Today's Date
It is very easy to set past 'n' number of years date from today date in kendo datepicker.
Find the small snippet below.
var toDate = new Date();
// Here 'n' Excludes number of years from today's Date.
var pastDate= new Date(toDate.setFullYear(toDate.getFullYear()- n));
$("#birthDayDate").kendoDatePicker({
max: pastDate
});
I have a column with data year&week. I would like to create a date axis based on this.
when i try to format the date, it is not working
I tried this:
var parseDate = d3.time.format("%Y-%W").parse;
d3.csv("temperatures_groups_2.csv", function(error, data) {
data.forEach(function(d,i) {
d.timestamp = parseDate(d.timestamp);
alert(d.timestamp);
d.temperature = +d.temperature;
}
});
i get null when the alert executes.
If i replace the same with d3.time.format("%Y-%m").parse, it works fine. It takes the month but not week no.
The csv contains data like this
timestamp,temperature
2013-01,19.5
2014-02,21.5
2015-03,20.5
2016-04,20.5
2017-05,21.25
The documentation says:
The following directives are not yet supported for parsing:
%U - week number of the year.
%W - week number of the year.
Hence you can't use D3 to parse these dates. You'll have to parse the date yourself or use another library, for example moment.js.
This works since version 3.2 and the documentation has been updated.
%U - week number of the year (Sunday as the first day of the week) as a decimal number [00,53].
%W - week number of the year (Monday as the first day of the week) as a decimal number [00,53].
However, the format in your question won't work because the parser requires a specific date to be specified. E.g. by "%a". It would be nice to assume Sunday in the absence of a date specifier, but that's for another PR.
I have a jquery calendar for the start date of a project.
Using Watir (automated browser driver, a gem for ruby), I have a set date that I would like to enter in.
The calendar start date is always today's date, whatever that may be for the day it is used. I was wondering if there was a way that ruby can process what today's date is, and use the specified date provided by the user, to calculate the difference of months between them.
Here is an example of the Calendar plugin: http://jqueryui.com/datepicker/
example:
today's date is 30/10/2012, if there was a project that were to start on the 20/12/2012, that would be 2 months from now, so 2 clicks on the next month button.
Is there a way I could do this?
Here is how I approached a similar situation with JSdatepicker:
$today = Time.now.strftime("%e").gsub(" ", "") #one digit day of month without leading space
#browser.text_field(:id => /dateAvailable/).click
Watir::Wait.until(60) {#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).exists?}
#browser.div(:id => /dateAvailable_popup_cal/).td(:text => $today).click
Set or grab the date.
Click the text_field that fires the JSDatePicker object
Wait until the calendar actually pops up
The current month is shown, so choose today's date number.
In your case, you also need to set the month. Whether prompting the user for this, or choosing "today", the theory is the same:
$month = Date::MONTHNAMES[Date.today.month] #etc
Pseudo-code making lots of assumptions (only future dates, month name shown on calendar as text, etc):
while !#jquerytablewindow.text.include?($month)
next_month_button.click
end
I don't see a specific advantage to my method versus counting each month, unless of course we add a month to the calendar one day and you still want your code to work!
You could do:
#End date converted to date object
specified_date = '20/12/2012'
end_date = Date.parse(specified_date)
#Start date (today - 30/10/2012)
today = Date.today
#Determine difference in months
number_of_months_up_to_today = (today.month + today.year * 12)
number_of_months_up_to_end = (end_date.month + end_date.year * 12)
clicks_required = number_of_months_up_to_end - number_of_months_up_to_today
#=> 2
Basically it is counting the number of months since the year 0 and then finding the difference.
I have a problem with getting the week of year. On my machine JDK 1.6.0.22 version is installed, on another machine 1.6.0.21. And both machines return different results:
(1.6.0.22) week is: 1 (1.6.0.21) week is: 52
For this code:
try {
Calendar current = new GregorianCalendar();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = df.parse("2010-12-28 19:04:38 GMT");
current.setTime(d);
int currentWeek = current.get(Calendar.WEEK_OF_YEAR);
System.out.println("week is: "currentWeek);
} catch (ParseException e) {
e.printStackTrace();
}
Why does JDK 1.6.0.22 give the wrong result?
This excerpt from the API documentation explains why this difference can occur:
Values calculated for the WEEK_OF_YEAR
field range from 1 to 53. Week 1 for a
year is the earliest seven day period
starting on getFirstDayOfWeek() that
contains at least
getMinimalDaysInFirstWeek() days from
that year. It thus depends on the
values of getMinimalDaysInFirstWeek(),
getFirstDayOfWeek(), and the day of
the week of January 1.
And from the source code of Calendar:
Both firstDayOfWeek and minimalDaysInFirstWeek are locale-dependent.
So it's the locale that determines this, not the time zone! Apparently, in some locales, week 1 of a year is considered to begin in the previous year. Try running this:
Calendar cal = new GregorianCalendar();
System.out.println(Locale.getDefault());
System.out.println(cal.getMinimalDaysInFirstWeek());
System.out.println(cal.getFirstDayOfWeek());
I bet that either you're running the different versions in different locales, or the locale data changed between these versions. The result "1" could even be the more correct one and due to a bug fix in the locale data.
Don't use the JDK date/time classes - use JODA Time instead.