Exctract year, month, day from datetime2Add (n) days to variable dateDate(time) ranges using `datetime2`numeric undefined in datetime2 packageHow do I extract a month name in datetime2?datetime2, norwegian day of week missing“datetime” to “datetime2”Change the case of datetime2 month returnHow to calculate previous month from today using datetime2?datetime2: scientific english formatdatetime2: Formatting the timeCalculating time spans with datetime2Changing date format in datetime2
Is "stainless" a bulk or a surface property of stainless steel?
How to dismiss intrusive questions from a colleague with whom I don't work?
The teacher logged me in as administrator for doing a short task, is the whole system now compromised?
Church Booleans
Why didn’t Doctor Strange stay in the original winning timeline?
How can I watch the 17th (or last, if less) line in files of a folder?
Why did MS-DOS applications built using Turbo Pascal fail to start with a division by zero error on faster systems?
Have only girls been born for a long time in this village?
Was Switzerland really impossible to invade during WW2?
Exctract year, month, day from datetime2
Why aren't RCS openings an issue for spacecraft heat shields?
Don't these experiments suggest that locality has to be abandoned in the quantum realm?
Can you feel passing through the sound barrier in an F-16?
Is there a limit on how long the casting (speaking aloud part of the spell) of Wish can be?
Why we don't have vaccination against all diseases which are caused by microbes?
Why did this happen to Thanos's ships at the end of "Avengers: Endgame"?
How do I request a longer than normal leave of absense period for my wedding?
Is it appropriate for a prospective landlord to ask me for my credit report?
How to persuade recruiters to send me the Job Description?
In what ways can a Non-paladin access Paladin spells?
Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?
Check in to 2 hotels at same location
Defense against attacks using dictionaries
Co-author responds to email by mistake cc'ing the EiC
Exctract year, month, day from datetime2
Add (n) days to variable dateDate(time) ranges using `datetime2`numeric undefined in datetime2 packageHow do I extract a month name in datetime2?datetime2, norwegian day of week missing“datetime” to “datetime2”Change the case of datetime2 month returnHow to calculate previous month from today using datetime2?datetime2: scientific english formatdatetime2: Formatting the timeCalculating time spans with datetime2Changing date format in datetime2
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have made a command that looks like this:
newcommand*numdash,--,
ExplSyntaxOn
NewDocumentCommanddateRangemmmmmm
str_case:nnF #1
#4
str_case:nnF #2
#5
str_case:nnF #3
#6 DTMdisplaydate#4#5#6-1 %
DTMordinal#3numdashDTMdisplaydate#4#5#6-1
DTMordinal#3~DTMmonthname#2%
numdashDTMdisplaydate#4#5#6-1
DTMdisplaydate#1#2#3-1numdashDTMdisplaydate#4#5#6-1
ExplSyntaxOff
It displays the duration from one date to the next in a succinct matter. See the image below.

However, I need to manipulate the end date by incrementing it by a few days.
Something like dateRange2016123120161231 + 1 does not work.
I found the following code
%https://tex.stackexchange.com/questions/318006/add-n-days-to-variable-date
DTMsavedateDeadLineDate2016-05-20
newcommandDeadLineDateExtend1
newcountdaycount
newcommanddueDate[1]%
DTMsaveddateoffsettojuliandayDeadLineDate#1daycount
DTMsavejuliandaynewDeadLineDatenumberdaycount
DTMusedatenewDeadLineDate
Which can add days to a DTMdate. My problem is making these two functions work together.
- Rewrite
dateRangeso that it inputs twoDTMdatesinstead of the plain numbers. - Exctract the year, month and date from
newDeadLineDateand insert it back intodateRange
So what I need help with is to first have two dates, increment one of them and display the daterange using dateRange function. Any help with 1. or 2. from the list above is greatly appreciated.
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
% https://tex.stackexchange.com/questions/390693/datetime-ranges-using-datetime2/390738
newcommand*numdash,--,
ExplSyntaxOn
NewDocumentCommanddateRangemmmmmm
str_case:nnF #1
#4
str_case:nnF #2
#5
str_case:nnF #3
#6 DTMdisplaydate#4#5#6-1 %
DTMordinal#3numdashDTMdisplaydate#4#5#6-1
DTMordinal#3~DTMmonthname#2%
numdashDTMdisplaydate#4#5#6-1
DTMdisplaydate#1#2#3-1numdashDTMdisplaydate#4#5#6-1
ExplSyntaxOff
%https://tex.stackexchange.com/questions/318006/add-n-days-to-variable-date
DTMsavedateDeadLineDate2016-05-20
newcommandDeadLineDateExtend1
newcountdaycount
newcommanddueDate[1]%
DTMsaveddateoffsettojuliandayDeadLineDate#1daycount
DTMsavejuliandaynewDeadLineDatenumberdaycount
DTMusedatenewDeadLineDate
begindocument
dateRange2016123120161231
dateRange2016123020161231
dateRange2016113120161231
dateRange2015123120161231
enddocument
datetime2
add a comment |
I have made a command that looks like this:
newcommand*numdash,--,
ExplSyntaxOn
NewDocumentCommanddateRangemmmmmm
str_case:nnF #1
#4
str_case:nnF #2
#5
str_case:nnF #3
#6 DTMdisplaydate#4#5#6-1 %
DTMordinal#3numdashDTMdisplaydate#4#5#6-1
DTMordinal#3~DTMmonthname#2%
numdashDTMdisplaydate#4#5#6-1
DTMdisplaydate#1#2#3-1numdashDTMdisplaydate#4#5#6-1
ExplSyntaxOff
It displays the duration from one date to the next in a succinct matter. See the image below.

However, I need to manipulate the end date by incrementing it by a few days.
Something like dateRange2016123120161231 + 1 does not work.
I found the following code
%https://tex.stackexchange.com/questions/318006/add-n-days-to-variable-date
DTMsavedateDeadLineDate2016-05-20
newcommandDeadLineDateExtend1
newcountdaycount
newcommanddueDate[1]%
DTMsaveddateoffsettojuliandayDeadLineDate#1daycount
DTMsavejuliandaynewDeadLineDatenumberdaycount
DTMusedatenewDeadLineDate
Which can add days to a DTMdate. My problem is making these two functions work together.
- Rewrite
dateRangeso that it inputs twoDTMdatesinstead of the plain numbers. - Exctract the year, month and date from
newDeadLineDateand insert it back intodateRange
So what I need help with is to first have two dates, increment one of them and display the daterange using dateRange function. Any help with 1. or 2. from the list above is greatly appreciated.
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
% https://tex.stackexchange.com/questions/390693/datetime-ranges-using-datetime2/390738
newcommand*numdash,--,
ExplSyntaxOn
NewDocumentCommanddateRangemmmmmm
str_case:nnF #1
#4
str_case:nnF #2
#5
str_case:nnF #3
#6 DTMdisplaydate#4#5#6-1 %
DTMordinal#3numdashDTMdisplaydate#4#5#6-1
DTMordinal#3~DTMmonthname#2%
numdashDTMdisplaydate#4#5#6-1
DTMdisplaydate#1#2#3-1numdashDTMdisplaydate#4#5#6-1
ExplSyntaxOff
%https://tex.stackexchange.com/questions/318006/add-n-days-to-variable-date
DTMsavedateDeadLineDate2016-05-20
newcommandDeadLineDateExtend1
newcountdaycount
newcommanddueDate[1]%
DTMsaveddateoffsettojuliandayDeadLineDate#1daycount
DTMsavejuliandaynewDeadLineDatenumberdaycount
DTMusedatenewDeadLineDate
begindocument
dateRange2016123120161231
dateRange2016123020161231
dateRange2016113120161231
dateRange2015123120161231
enddocument
datetime2
You did not say how you want theDeadLineDateExtendto be handled in the desired function. Do you want to modifydateRangeto always addDeadLineDateExtendto the end date? Do you want it to accept the number of additional days as an additional argument (maybe an optional one, defaulting to0)? Do you want a new function to deal with the extension?.. In other words, what is the desired interface (API)?
– frougon
7 hours ago
add a comment |
I have made a command that looks like this:
newcommand*numdash,--,
ExplSyntaxOn
NewDocumentCommanddateRangemmmmmm
str_case:nnF #1
#4
str_case:nnF #2
#5
str_case:nnF #3
#6 DTMdisplaydate#4#5#6-1 %
DTMordinal#3numdashDTMdisplaydate#4#5#6-1
DTMordinal#3~DTMmonthname#2%
numdashDTMdisplaydate#4#5#6-1
DTMdisplaydate#1#2#3-1numdashDTMdisplaydate#4#5#6-1
ExplSyntaxOff
It displays the duration from one date to the next in a succinct matter. See the image below.

However, I need to manipulate the end date by incrementing it by a few days.
Something like dateRange2016123120161231 + 1 does not work.
I found the following code
%https://tex.stackexchange.com/questions/318006/add-n-days-to-variable-date
DTMsavedateDeadLineDate2016-05-20
newcommandDeadLineDateExtend1
newcountdaycount
newcommanddueDate[1]%
DTMsaveddateoffsettojuliandayDeadLineDate#1daycount
DTMsavejuliandaynewDeadLineDatenumberdaycount
DTMusedatenewDeadLineDate
Which can add days to a DTMdate. My problem is making these two functions work together.
- Rewrite
dateRangeso that it inputs twoDTMdatesinstead of the plain numbers. - Exctract the year, month and date from
newDeadLineDateand insert it back intodateRange
So what I need help with is to first have two dates, increment one of them and display the daterange using dateRange function. Any help with 1. or 2. from the list above is greatly appreciated.
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
% https://tex.stackexchange.com/questions/390693/datetime-ranges-using-datetime2/390738
newcommand*numdash,--,
ExplSyntaxOn
NewDocumentCommanddateRangemmmmmm
str_case:nnF #1
#4
str_case:nnF #2
#5
str_case:nnF #3
#6 DTMdisplaydate#4#5#6-1 %
DTMordinal#3numdashDTMdisplaydate#4#5#6-1
DTMordinal#3~DTMmonthname#2%
numdashDTMdisplaydate#4#5#6-1
DTMdisplaydate#1#2#3-1numdashDTMdisplaydate#4#5#6-1
ExplSyntaxOff
%https://tex.stackexchange.com/questions/318006/add-n-days-to-variable-date
DTMsavedateDeadLineDate2016-05-20
newcommandDeadLineDateExtend1
newcountdaycount
newcommanddueDate[1]%
DTMsaveddateoffsettojuliandayDeadLineDate#1daycount
DTMsavejuliandaynewDeadLineDatenumberdaycount
DTMusedatenewDeadLineDate
begindocument
dateRange2016123120161231
dateRange2016123020161231
dateRange2016113120161231
dateRange2015123120161231
enddocument
datetime2
I have made a command that looks like this:
newcommand*numdash,--,
ExplSyntaxOn
NewDocumentCommanddateRangemmmmmm
str_case:nnF #1
#4
str_case:nnF #2
#5
str_case:nnF #3
#6 DTMdisplaydate#4#5#6-1 %
DTMordinal#3numdashDTMdisplaydate#4#5#6-1
DTMordinal#3~DTMmonthname#2%
numdashDTMdisplaydate#4#5#6-1
DTMdisplaydate#1#2#3-1numdashDTMdisplaydate#4#5#6-1
ExplSyntaxOff
It displays the duration from one date to the next in a succinct matter. See the image below.

However, I need to manipulate the end date by incrementing it by a few days.
Something like dateRange2016123120161231 + 1 does not work.
I found the following code
%https://tex.stackexchange.com/questions/318006/add-n-days-to-variable-date
DTMsavedateDeadLineDate2016-05-20
newcommandDeadLineDateExtend1
newcountdaycount
newcommanddueDate[1]%
DTMsaveddateoffsettojuliandayDeadLineDate#1daycount
DTMsavejuliandaynewDeadLineDatenumberdaycount
DTMusedatenewDeadLineDate
Which can add days to a DTMdate. My problem is making these two functions work together.
- Rewrite
dateRangeso that it inputs twoDTMdatesinstead of the plain numbers. - Exctract the year, month and date from
newDeadLineDateand insert it back intodateRange
So what I need help with is to first have two dates, increment one of them and display the daterange using dateRange function. Any help with 1. or 2. from the list above is greatly appreciated.
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
% https://tex.stackexchange.com/questions/390693/datetime-ranges-using-datetime2/390738
newcommand*numdash,--,
ExplSyntaxOn
NewDocumentCommanddateRangemmmmmm
str_case:nnF #1
#4
str_case:nnF #2
#5
str_case:nnF #3
#6 DTMdisplaydate#4#5#6-1 %
DTMordinal#3numdashDTMdisplaydate#4#5#6-1
DTMordinal#3~DTMmonthname#2%
numdashDTMdisplaydate#4#5#6-1
DTMdisplaydate#1#2#3-1numdashDTMdisplaydate#4#5#6-1
ExplSyntaxOff
%https://tex.stackexchange.com/questions/318006/add-n-days-to-variable-date
DTMsavedateDeadLineDate2016-05-20
newcommandDeadLineDateExtend1
newcountdaycount
newcommanddueDate[1]%
DTMsaveddateoffsettojuliandayDeadLineDate#1daycount
DTMsavejuliandaynewDeadLineDatenumberdaycount
DTMusedatenewDeadLineDate
begindocument
dateRange2016123120161231
dateRange2016123020161231
dateRange2016113120161231
dateRange2015123120161231
enddocument
datetime2
datetime2
asked 8 hours ago
N3buchadnezzarN3buchadnezzar
4,7855 gold badges42 silver badges99 bronze badges
4,7855 gold badges42 silver badges99 bronze badges
You did not say how you want theDeadLineDateExtendto be handled in the desired function. Do you want to modifydateRangeto always addDeadLineDateExtendto the end date? Do you want it to accept the number of additional days as an additional argument (maybe an optional one, defaulting to0)? Do you want a new function to deal with the extension?.. In other words, what is the desired interface (API)?
– frougon
7 hours ago
add a comment |
You did not say how you want theDeadLineDateExtendto be handled in the desired function. Do you want to modifydateRangeto always addDeadLineDateExtendto the end date? Do you want it to accept the number of additional days as an additional argument (maybe an optional one, defaulting to0)? Do you want a new function to deal with the extension?.. In other words, what is the desired interface (API)?
– frougon
7 hours ago
You did not say how you want the
DeadLineDateExtend to be handled in the desired function. Do you want to modify dateRange to always add DeadLineDateExtend to the end date? Do you want it to accept the number of additional days as an additional argument (maybe an optional one, defaulting to 0)? Do you want a new function to deal with the extension?.. In other words, what is the desired interface (API)?– frougon
7 hours ago
You did not say how you want the
DeadLineDateExtend to be handled in the desired function. Do you want to modify dateRange to always add DeadLineDateExtend to the end date? Do you want it to accept the number of additional days as an additional argument (maybe an optional one, defaulting to 0)? Do you want a new function to deal with the extension?.. In other words, what is the desired interface (API)?– frougon
7 hours ago
add a comment |
1 Answer
1
active
oldest
votes
Extracting the year, month and day from a named datetime2 date
As you probably noticed in your code sample where the commands DTMsavedate and DTMsavejulianday are used, the datetime2 package can save dates in special stores identified by a 〈name〉. Quoting section Storing and Using Dates and Times of the datetime2 manual:
In the commands below, the 〈name〉 (no active characters) is a name that uniquely identifies the information.
When you have a date in such a store, as is the case after executing DTMsavejulianday〈name〉〈number〉, you can extract the corresponding year, month and day using the commands DTMfetchyear, DTMfetchmonth and DTMfetchday. Each of these commands takes a 〈name〉 argument and expands to the corresponding number. In other words, DTMfetchyear〈name〉 expands to the year, DTMfetchmonth〈name〉 to the month and DTMfetchday〈name〉 to the day. The rest is essentially plumbing work. :-)
Connecting the different parts
I propose a dateRange command that behaves like yours, except that:
it accepts an optional argument (defaulting to 0) that specifies the number of days you want to add to the end date before displaying the resulting range;
it uses a no-break space between the day ordinal and the month name (
~is a normal space underExplSyntaxOnregime, but I believe you wanted a no-break space);it uses integer comparisons, so that
01is considered the same as1,02the same as2, etc. for all day, month and year arguments (if you don't want that, usestr_if_eq:nnTFas in the first revision of this answer, but I don't see any good reason to do so).
I reindented the function and used int_compare:nNnTF instead of str_case:nnF to make the code easier to read and address the third point above.
I also defined a code-level function nebuch_display_date_range:nnnnnn to contain this reworked code, because it is much easier to reuse in different situations this way. This allows us to generate a variant nebuch_display_date_range:nnnxxx that fully expands the three last arguments before passing them to the base function nebuch_display_date_range:nnnnnn (full expansion as done by edef). This only requires a simple line of code:
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
Using the variant, it's easy to pass the result of the temporary date calculation (which adds the offset) to the base function nebuch_display_date_range:nnnnnn, because DTMfetchday, DTMfetchmonth and DTMfetchyear are all expandable functions (see the documentation of datetime2).
The computation of the offset date is also implemented in a code-level function, namely nebuch_compute_offset_date:nnnnn, to make it easily reusable by other code (and it is used in my implementation of dateRange).
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
newcommand*numdash,--,
ExplSyntaxOn
% Same as your dateRange, but the code-level API allows the creation of
% variants with cs_generate_variant:Nn.
cs_new_protected:Npn nebuch_display_date_range:nnnnnn #1#2#3#4#5#6
int_compare:nNnTF #1 = #4
int_compare:nNnTF #2 = #5
int_compare:nNnTF #3 = #6
DTMdisplaydate #4 #5 #6 -1
DTMordinal #3 numdash
DTMdisplaydate #4 #5 #6 -1
% I replaced ~ with nobreakspace here because of ExplSyntaxOn
DTMordinal #3 nobreakspace DTMmonthname #2 numdash
DTMdisplaydate #4 #5 #6 -1
DTMdisplaydate #1 #2 #3 -1 numdash
DTMdisplaydate #4 #5 #6 -1
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
newcount nebuch_tmp_count
% #1, #2, #3: year, month, day
% #4: number of days (offset)
% #5: <name> (in the sense of datetime2) used to store the resulting date
cs_new_protected:Npn nebuch_compute_offset_date:nnnnn #1#2#3#4#5
DTMsavedate nebuch_compute_offset_date_tmp_date #1-#2-#3
DTMsaveddateoffsettojulianday
nebuch_compute_offset_date_tmp_date #4 nebuch_tmp_count
DTMsavejulianday #5 number nebuch_tmp_count
NewDocumentCommand dateRange O0 m m m m m m
nebuch_compute_offset_date:nnnnn #5 #6 #7 #1
nebuch_dateRange_tmp_date
nebuch_display_date_range:nnnxxx #2 #3 #4
DTMfetchyear nebuch_dateRange_tmp_date
DTMfetchmonth nebuch_dateRange_tmp_date
DTMfetchday nebuch_dateRange_tmp_date
ExplSyntaxOff
begindocument
dateRange2016123120161231par
dateRange2016123020161231par
dateRange2016113120161231par
dateRange2015123120161231
bigskip
dateRange[3]2016123120161231par
dateRange[3]2016123120161228par
dateRange[3]2016123020161231par
dateRange[3]2016123020161228par
dateRange[31]2016113120161231par
dateRange[31]2016113120161130par
dateRange[365]2015123120161231par
dateRange[365]2015123120160101
bigskip
dateRange20160272016207
enddocument

add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f505123%2fexctract-year-month-day-from-datetime2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Extracting the year, month and day from a named datetime2 date
As you probably noticed in your code sample where the commands DTMsavedate and DTMsavejulianday are used, the datetime2 package can save dates in special stores identified by a 〈name〉. Quoting section Storing and Using Dates and Times of the datetime2 manual:
In the commands below, the 〈name〉 (no active characters) is a name that uniquely identifies the information.
When you have a date in such a store, as is the case after executing DTMsavejulianday〈name〉〈number〉, you can extract the corresponding year, month and day using the commands DTMfetchyear, DTMfetchmonth and DTMfetchday. Each of these commands takes a 〈name〉 argument and expands to the corresponding number. In other words, DTMfetchyear〈name〉 expands to the year, DTMfetchmonth〈name〉 to the month and DTMfetchday〈name〉 to the day. The rest is essentially plumbing work. :-)
Connecting the different parts
I propose a dateRange command that behaves like yours, except that:
it accepts an optional argument (defaulting to 0) that specifies the number of days you want to add to the end date before displaying the resulting range;
it uses a no-break space between the day ordinal and the month name (
~is a normal space underExplSyntaxOnregime, but I believe you wanted a no-break space);it uses integer comparisons, so that
01is considered the same as1,02the same as2, etc. for all day, month and year arguments (if you don't want that, usestr_if_eq:nnTFas in the first revision of this answer, but I don't see any good reason to do so).
I reindented the function and used int_compare:nNnTF instead of str_case:nnF to make the code easier to read and address the third point above.
I also defined a code-level function nebuch_display_date_range:nnnnnn to contain this reworked code, because it is much easier to reuse in different situations this way. This allows us to generate a variant nebuch_display_date_range:nnnxxx that fully expands the three last arguments before passing them to the base function nebuch_display_date_range:nnnnnn (full expansion as done by edef). This only requires a simple line of code:
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
Using the variant, it's easy to pass the result of the temporary date calculation (which adds the offset) to the base function nebuch_display_date_range:nnnnnn, because DTMfetchday, DTMfetchmonth and DTMfetchyear are all expandable functions (see the documentation of datetime2).
The computation of the offset date is also implemented in a code-level function, namely nebuch_compute_offset_date:nnnnn, to make it easily reusable by other code (and it is used in my implementation of dateRange).
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
newcommand*numdash,--,
ExplSyntaxOn
% Same as your dateRange, but the code-level API allows the creation of
% variants with cs_generate_variant:Nn.
cs_new_protected:Npn nebuch_display_date_range:nnnnnn #1#2#3#4#5#6
int_compare:nNnTF #1 = #4
int_compare:nNnTF #2 = #5
int_compare:nNnTF #3 = #6
DTMdisplaydate #4 #5 #6 -1
DTMordinal #3 numdash
DTMdisplaydate #4 #5 #6 -1
% I replaced ~ with nobreakspace here because of ExplSyntaxOn
DTMordinal #3 nobreakspace DTMmonthname #2 numdash
DTMdisplaydate #4 #5 #6 -1
DTMdisplaydate #1 #2 #3 -1 numdash
DTMdisplaydate #4 #5 #6 -1
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
newcount nebuch_tmp_count
% #1, #2, #3: year, month, day
% #4: number of days (offset)
% #5: <name> (in the sense of datetime2) used to store the resulting date
cs_new_protected:Npn nebuch_compute_offset_date:nnnnn #1#2#3#4#5
DTMsavedate nebuch_compute_offset_date_tmp_date #1-#2-#3
DTMsaveddateoffsettojulianday
nebuch_compute_offset_date_tmp_date #4 nebuch_tmp_count
DTMsavejulianday #5 number nebuch_tmp_count
NewDocumentCommand dateRange O0 m m m m m m
nebuch_compute_offset_date:nnnnn #5 #6 #7 #1
nebuch_dateRange_tmp_date
nebuch_display_date_range:nnnxxx #2 #3 #4
DTMfetchyear nebuch_dateRange_tmp_date
DTMfetchmonth nebuch_dateRange_tmp_date
DTMfetchday nebuch_dateRange_tmp_date
ExplSyntaxOff
begindocument
dateRange2016123120161231par
dateRange2016123020161231par
dateRange2016113120161231par
dateRange2015123120161231
bigskip
dateRange[3]2016123120161231par
dateRange[3]2016123120161228par
dateRange[3]2016123020161231par
dateRange[3]2016123020161228par
dateRange[31]2016113120161231par
dateRange[31]2016113120161130par
dateRange[365]2015123120161231par
dateRange[365]2015123120160101
bigskip
dateRange20160272016207
enddocument

add a comment |
Extracting the year, month and day from a named datetime2 date
As you probably noticed in your code sample where the commands DTMsavedate and DTMsavejulianday are used, the datetime2 package can save dates in special stores identified by a 〈name〉. Quoting section Storing and Using Dates and Times of the datetime2 manual:
In the commands below, the 〈name〉 (no active characters) is a name that uniquely identifies the information.
When you have a date in such a store, as is the case after executing DTMsavejulianday〈name〉〈number〉, you can extract the corresponding year, month and day using the commands DTMfetchyear, DTMfetchmonth and DTMfetchday. Each of these commands takes a 〈name〉 argument and expands to the corresponding number. In other words, DTMfetchyear〈name〉 expands to the year, DTMfetchmonth〈name〉 to the month and DTMfetchday〈name〉 to the day. The rest is essentially plumbing work. :-)
Connecting the different parts
I propose a dateRange command that behaves like yours, except that:
it accepts an optional argument (defaulting to 0) that specifies the number of days you want to add to the end date before displaying the resulting range;
it uses a no-break space between the day ordinal and the month name (
~is a normal space underExplSyntaxOnregime, but I believe you wanted a no-break space);it uses integer comparisons, so that
01is considered the same as1,02the same as2, etc. for all day, month and year arguments (if you don't want that, usestr_if_eq:nnTFas in the first revision of this answer, but I don't see any good reason to do so).
I reindented the function and used int_compare:nNnTF instead of str_case:nnF to make the code easier to read and address the third point above.
I also defined a code-level function nebuch_display_date_range:nnnnnn to contain this reworked code, because it is much easier to reuse in different situations this way. This allows us to generate a variant nebuch_display_date_range:nnnxxx that fully expands the three last arguments before passing them to the base function nebuch_display_date_range:nnnnnn (full expansion as done by edef). This only requires a simple line of code:
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
Using the variant, it's easy to pass the result of the temporary date calculation (which adds the offset) to the base function nebuch_display_date_range:nnnnnn, because DTMfetchday, DTMfetchmonth and DTMfetchyear are all expandable functions (see the documentation of datetime2).
The computation of the offset date is also implemented in a code-level function, namely nebuch_compute_offset_date:nnnnn, to make it easily reusable by other code (and it is used in my implementation of dateRange).
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
newcommand*numdash,--,
ExplSyntaxOn
% Same as your dateRange, but the code-level API allows the creation of
% variants with cs_generate_variant:Nn.
cs_new_protected:Npn nebuch_display_date_range:nnnnnn #1#2#3#4#5#6
int_compare:nNnTF #1 = #4
int_compare:nNnTF #2 = #5
int_compare:nNnTF #3 = #6
DTMdisplaydate #4 #5 #6 -1
DTMordinal #3 numdash
DTMdisplaydate #4 #5 #6 -1
% I replaced ~ with nobreakspace here because of ExplSyntaxOn
DTMordinal #3 nobreakspace DTMmonthname #2 numdash
DTMdisplaydate #4 #5 #6 -1
DTMdisplaydate #1 #2 #3 -1 numdash
DTMdisplaydate #4 #5 #6 -1
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
newcount nebuch_tmp_count
% #1, #2, #3: year, month, day
% #4: number of days (offset)
% #5: <name> (in the sense of datetime2) used to store the resulting date
cs_new_protected:Npn nebuch_compute_offset_date:nnnnn #1#2#3#4#5
DTMsavedate nebuch_compute_offset_date_tmp_date #1-#2-#3
DTMsaveddateoffsettojulianday
nebuch_compute_offset_date_tmp_date #4 nebuch_tmp_count
DTMsavejulianday #5 number nebuch_tmp_count
NewDocumentCommand dateRange O0 m m m m m m
nebuch_compute_offset_date:nnnnn #5 #6 #7 #1
nebuch_dateRange_tmp_date
nebuch_display_date_range:nnnxxx #2 #3 #4
DTMfetchyear nebuch_dateRange_tmp_date
DTMfetchmonth nebuch_dateRange_tmp_date
DTMfetchday nebuch_dateRange_tmp_date
ExplSyntaxOff
begindocument
dateRange2016123120161231par
dateRange2016123020161231par
dateRange2016113120161231par
dateRange2015123120161231
bigskip
dateRange[3]2016123120161231par
dateRange[3]2016123120161228par
dateRange[3]2016123020161231par
dateRange[3]2016123020161228par
dateRange[31]2016113120161231par
dateRange[31]2016113120161130par
dateRange[365]2015123120161231par
dateRange[365]2015123120160101
bigskip
dateRange20160272016207
enddocument

add a comment |
Extracting the year, month and day from a named datetime2 date
As you probably noticed in your code sample where the commands DTMsavedate and DTMsavejulianday are used, the datetime2 package can save dates in special stores identified by a 〈name〉. Quoting section Storing and Using Dates and Times of the datetime2 manual:
In the commands below, the 〈name〉 (no active characters) is a name that uniquely identifies the information.
When you have a date in such a store, as is the case after executing DTMsavejulianday〈name〉〈number〉, you can extract the corresponding year, month and day using the commands DTMfetchyear, DTMfetchmonth and DTMfetchday. Each of these commands takes a 〈name〉 argument and expands to the corresponding number. In other words, DTMfetchyear〈name〉 expands to the year, DTMfetchmonth〈name〉 to the month and DTMfetchday〈name〉 to the day. The rest is essentially plumbing work. :-)
Connecting the different parts
I propose a dateRange command that behaves like yours, except that:
it accepts an optional argument (defaulting to 0) that specifies the number of days you want to add to the end date before displaying the resulting range;
it uses a no-break space between the day ordinal and the month name (
~is a normal space underExplSyntaxOnregime, but I believe you wanted a no-break space);it uses integer comparisons, so that
01is considered the same as1,02the same as2, etc. for all day, month and year arguments (if you don't want that, usestr_if_eq:nnTFas in the first revision of this answer, but I don't see any good reason to do so).
I reindented the function and used int_compare:nNnTF instead of str_case:nnF to make the code easier to read and address the third point above.
I also defined a code-level function nebuch_display_date_range:nnnnnn to contain this reworked code, because it is much easier to reuse in different situations this way. This allows us to generate a variant nebuch_display_date_range:nnnxxx that fully expands the three last arguments before passing them to the base function nebuch_display_date_range:nnnnnn (full expansion as done by edef). This only requires a simple line of code:
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
Using the variant, it's easy to pass the result of the temporary date calculation (which adds the offset) to the base function nebuch_display_date_range:nnnnnn, because DTMfetchday, DTMfetchmonth and DTMfetchyear are all expandable functions (see the documentation of datetime2).
The computation of the offset date is also implemented in a code-level function, namely nebuch_compute_offset_date:nnnnn, to make it easily reusable by other code (and it is used in my implementation of dateRange).
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
newcommand*numdash,--,
ExplSyntaxOn
% Same as your dateRange, but the code-level API allows the creation of
% variants with cs_generate_variant:Nn.
cs_new_protected:Npn nebuch_display_date_range:nnnnnn #1#2#3#4#5#6
int_compare:nNnTF #1 = #4
int_compare:nNnTF #2 = #5
int_compare:nNnTF #3 = #6
DTMdisplaydate #4 #5 #6 -1
DTMordinal #3 numdash
DTMdisplaydate #4 #5 #6 -1
% I replaced ~ with nobreakspace here because of ExplSyntaxOn
DTMordinal #3 nobreakspace DTMmonthname #2 numdash
DTMdisplaydate #4 #5 #6 -1
DTMdisplaydate #1 #2 #3 -1 numdash
DTMdisplaydate #4 #5 #6 -1
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
newcount nebuch_tmp_count
% #1, #2, #3: year, month, day
% #4: number of days (offset)
% #5: <name> (in the sense of datetime2) used to store the resulting date
cs_new_protected:Npn nebuch_compute_offset_date:nnnnn #1#2#3#4#5
DTMsavedate nebuch_compute_offset_date_tmp_date #1-#2-#3
DTMsaveddateoffsettojulianday
nebuch_compute_offset_date_tmp_date #4 nebuch_tmp_count
DTMsavejulianday #5 number nebuch_tmp_count
NewDocumentCommand dateRange O0 m m m m m m
nebuch_compute_offset_date:nnnnn #5 #6 #7 #1
nebuch_dateRange_tmp_date
nebuch_display_date_range:nnnxxx #2 #3 #4
DTMfetchyear nebuch_dateRange_tmp_date
DTMfetchmonth nebuch_dateRange_tmp_date
DTMfetchday nebuch_dateRange_tmp_date
ExplSyntaxOff
begindocument
dateRange2016123120161231par
dateRange2016123020161231par
dateRange2016113120161231par
dateRange2015123120161231
bigskip
dateRange[3]2016123120161231par
dateRange[3]2016123120161228par
dateRange[3]2016123020161231par
dateRange[3]2016123020161228par
dateRange[31]2016113120161231par
dateRange[31]2016113120161130par
dateRange[365]2015123120161231par
dateRange[365]2015123120160101
bigskip
dateRange20160272016207
enddocument

Extracting the year, month and day from a named datetime2 date
As you probably noticed in your code sample where the commands DTMsavedate and DTMsavejulianday are used, the datetime2 package can save dates in special stores identified by a 〈name〉. Quoting section Storing and Using Dates and Times of the datetime2 manual:
In the commands below, the 〈name〉 (no active characters) is a name that uniquely identifies the information.
When you have a date in such a store, as is the case after executing DTMsavejulianday〈name〉〈number〉, you can extract the corresponding year, month and day using the commands DTMfetchyear, DTMfetchmonth and DTMfetchday. Each of these commands takes a 〈name〉 argument and expands to the corresponding number. In other words, DTMfetchyear〈name〉 expands to the year, DTMfetchmonth〈name〉 to the month and DTMfetchday〈name〉 to the day. The rest is essentially plumbing work. :-)
Connecting the different parts
I propose a dateRange command that behaves like yours, except that:
it accepts an optional argument (defaulting to 0) that specifies the number of days you want to add to the end date before displaying the resulting range;
it uses a no-break space between the day ordinal and the month name (
~is a normal space underExplSyntaxOnregime, but I believe you wanted a no-break space);it uses integer comparisons, so that
01is considered the same as1,02the same as2, etc. for all day, month and year arguments (if you don't want that, usestr_if_eq:nnTFas in the first revision of this answer, but I don't see any good reason to do so).
I reindented the function and used int_compare:nNnTF instead of str_case:nnF to make the code easier to read and address the third point above.
I also defined a code-level function nebuch_display_date_range:nnnnnn to contain this reworked code, because it is much easier to reuse in different situations this way. This allows us to generate a variant nebuch_display_date_range:nnnxxx that fully expands the three last arguments before passing them to the base function nebuch_display_date_range:nnnnnn (full expansion as done by edef). This only requires a simple line of code:
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
Using the variant, it's easy to pass the result of the temporary date calculation (which adds the offset) to the base function nebuch_display_date_range:nnnnnn, because DTMfetchday, DTMfetchmonth and DTMfetchyear are all expandable functions (see the documentation of datetime2).
The computation of the offset date is also implemented in a code-level function, namely nebuch_compute_offset_date:nnnnn, to make it easily reusable by other code (and it is used in my implementation of dateRange).
documentclassarticle
usepackage[english]babel
usepackage[en-GB,calc]datetime2
usepackagexparse
newcommand*numdash,--,
ExplSyntaxOn
% Same as your dateRange, but the code-level API allows the creation of
% variants with cs_generate_variant:Nn.
cs_new_protected:Npn nebuch_display_date_range:nnnnnn #1#2#3#4#5#6
int_compare:nNnTF #1 = #4
int_compare:nNnTF #2 = #5
int_compare:nNnTF #3 = #6
DTMdisplaydate #4 #5 #6 -1
DTMordinal #3 numdash
DTMdisplaydate #4 #5 #6 -1
% I replaced ~ with nobreakspace here because of ExplSyntaxOn
DTMordinal #3 nobreakspace DTMmonthname #2 numdash
DTMdisplaydate #4 #5 #6 -1
DTMdisplaydate #1 #2 #3 -1 numdash
DTMdisplaydate #4 #5 #6 -1
cs_generate_variant:Nn nebuch_display_date_range:nnnnnn nnnxxx
newcount nebuch_tmp_count
% #1, #2, #3: year, month, day
% #4: number of days (offset)
% #5: <name> (in the sense of datetime2) used to store the resulting date
cs_new_protected:Npn nebuch_compute_offset_date:nnnnn #1#2#3#4#5
DTMsavedate nebuch_compute_offset_date_tmp_date #1-#2-#3
DTMsaveddateoffsettojulianday
nebuch_compute_offset_date_tmp_date #4 nebuch_tmp_count
DTMsavejulianday #5 number nebuch_tmp_count
NewDocumentCommand dateRange O0 m m m m m m
nebuch_compute_offset_date:nnnnn #5 #6 #7 #1
nebuch_dateRange_tmp_date
nebuch_display_date_range:nnnxxx #2 #3 #4
DTMfetchyear nebuch_dateRange_tmp_date
DTMfetchmonth nebuch_dateRange_tmp_date
DTMfetchday nebuch_dateRange_tmp_date
ExplSyntaxOff
begindocument
dateRange2016123120161231par
dateRange2016123020161231par
dateRange2016113120161231par
dateRange2015123120161231
bigskip
dateRange[3]2016123120161231par
dateRange[3]2016123120161228par
dateRange[3]2016123020161231par
dateRange[3]2016123020161228par
dateRange[31]2016113120161231par
dateRange[31]2016113120161130par
dateRange[365]2015123120161231par
dateRange[365]2015123120160101
bigskip
dateRange20160272016207
enddocument

edited 1 hour ago
answered 6 hours ago
frougonfrougon
7,9401 gold badge13 silver badges26 bronze badges
7,9401 gold badge13 silver badges26 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f505123%2fexctract-year-month-day-from-datetime2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You did not say how you want the
DeadLineDateExtendto be handled in the desired function. Do you want to modifydateRangeto always addDeadLineDateExtendto the end date? Do you want it to accept the number of additional days as an additional argument (maybe an optional one, defaulting to0)? Do you want a new function to deal with the extension?.. In other words, what is the desired interface (API)?– frougon
7 hours ago