[Tex/LaTex] Hack of .bst to add urldate in @misc in BibTex with natbib

bibtexisodatenatbiburlbsturls

Trying to add urldate to my bst-file.

I have the entry as:

ENTRY
  { address
    ...
    url
    urldate
    ...
    year
  }

With the two functions defining url and urldate:

FUNCTION {format.url}
{ url duplicate$ empty$
    { pop$ "" }
    { "\url{" swap$ * "}" * }
  if$
}

FUNCTION {format.urldate}
{ urldate duplicate$ empty$
    { pop$ "" }
    { "~(Accessed: " * urldate * ")" * }
  if$
}

And last, the function misc:

FUNCTION {misc}
{ output.bibitem
  format.authors output
  author format.key output
  format.date "year" output.check
  date.block
  format.title output
  new.block
  howpublished "howpublished" bibinfo.check output
  format.doi output
  new.block
  format.annote output
  format.eprint output
  format.url output
  format.urldate output
  fin.entry
}

And a MWE:

\documentclass{article}
\usepackage[main=UKenglish,danish]{babel}
\usepackage{natbib}
\usepackage{filecontents,url}
\begin{filecontents*}{\jobname.bib}
@misc{DenTool2015,
author = {{The Engineering ToolBox}},
title = {{Density and Specific Weight of Air at Standard Atmospheric Pressure -- SI Units}},
url = {http://www.engineeringtoolbox.com/air-density-specific-weight-d_600.html},
urldate = {2015-03-30},
year = {2015}
}
\end{filecontents*}

\begin{document}
\cite{DenTool2015}
\bibliographystyle{bibstyle}
\bibliography{\jobname}
\end{document}

Here is the bst-file:
https://db.tt/NUW5iXqF

I get this:
PDF

Here you can see two urldate entries. Hope you can help, removing the first one.
/Tobias


Comment:

After the help from Boris, I got the urldate to work. If you want to edit the date-format to UK or EU standards, use the isodate packages.

Ex:

FUNCTION {format.url}
{ url duplicate$ empty$
    { pop$ "" }
    { "\url{" swap$ * "}" * 
        urldate duplicate$ empty$
        { pop$ }
        { "~(Date last accessed:~\numdate\printdate{"  swap$ * "})" * *}
      if$
    }
  if$
}

will give:
pdf

Best Answer

You have two errors in your hack.

First, you leave urldate on stack after your if$ statement. This is how you get two dates. You need to use this instance with swap$ instead of putting the third instance of urldate on stack:

FUNCTION {format.urldate}
{ urldate duplicate$ empty$
    { pop$ "" }
    { "~(Accessed: " swap$ * ")" * }
  if$
}

However, if you make this change, you will see that url and date are separated by comma: url, (Accessed: urldate). I guess it is not what you want. So I suggest deleting format.urldate altogether and putting urldate into format.url instead like this:

FUNCTION {format.url}
{ url duplicate$ empty$
    { pop$ "" }
    { "\url{" swap$ * "}" * 
        urldate duplicate$ empty$
        { pop$ }
        { "~(Accessed: "  swap$ * ")" * *}
      if$
    }
  if$
}

This takes care of the case where you have urldate but not url (a good software must gracefully deal with users' mistakes).

The result is

The Engineering ToolBox, 2015: Density and Specific Weight of Air at Standard Atmospheric Pressure ­ SI Units. http://www.engineeringtoolbox.com/air-density-specific-weight-d_600.html (Accessed: 2015-03-30).