I want to change date format for date string
Ex. /changeformat{'06/23/2016'} and result = 23 June 2016
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this community\documentclass{article}
\def\changeformat#1{\xchangeformat#1\relax}
\def\xchangeformat#1/#2/#3\relax{%
#2 %
\ifcase#1 \or
January\or February\or March\or April\or May\or June\or July\or
August\or September\or October\or November\or December\fi
\ #3}
\begin{document}
\changeformat{06/23/2016} and result = 23 June 2016
\end{document}
Compile with lualatex. Uses standard os.date()
library
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[margin=1in]{geometry}
\usepackage{luacode}
\begin{luacode*}
function conv(t)
local m = tonumber(t:sub(1,2))
local d = tonumber(t:sub(4,5))
local y = tonumber(t:sub(7,10))
whc = os.time({year=y, month=m, day=d})
tex.print(os.date("%d %B %Y",whc))
end
\end{luacode*}
\begin{document}
06/23/2016 is \directlua{conv('06/23/2016')}
12/30/2016 is \directlua{conv('12/30/2016')}
\end{document}
This solution uses datetime2
, so you can apply the official format for a given country, or any format you choose, with day of week.
Also, it works with any seperator of your choice, so if you can use 06/23/2016
,06.23.2016
or whatever you throw at it.
The format-changing is done with xstring
.
Output
Code
\documentclass[11pt, australian]{article}
\usepackage{babel}
\usepackage{xstring}
\usepackage[useregional, calc]{datetime2}
\newcommand{\changeformat}[1]{%
\StrRight{#1}{4}[\myyear]%
\StrLeft{#1}{2}[\mymonth]%
\StrMid{#1}{4}{5}[\mydate]%
\DTMdate{\myyear-\mymonth-\mydate}%
}
\DTMusemodule{british}{en-GB} % Only neaded for extra languages.
\begin{document}
\changeformat{06/23/2016}
\DTMsetdatestyle{iso}
\changeformat{06/23/2016}
\DTMsetdatestyle{ddmmyyyy}
\changeformat{06.23.2016}
\DTMlangsetup[en-GB]{abbr}
\DTMsetstyle{en-GB}
\DTMsetup{showdow}
\changeformat{06/23/2016}
\end{document}
\documentclass{scrartcl}
\renewcommand*\today{\number\day\space
\ifcase\month\or January\or February\or March\or April\or May\or June\or
July\or August\or September\or October\or November\or December\fi\space
\number\year}
\begin{document}
\today
\end{document}