Returns a date formatted according to a given format string.

View versions (2)

Interface

#include <codecogs/units/date/date_str.h>

using namespace Units::Date;

Returns a string formatted according to the given format string using the given integer timestamp or the current local time if no timestamp is given.

\copydoc DateString DateString::fill

The string returned by this function is dynamically allocated and it is necessary for the calling code to deallocate it with a delete statement otherwise a memory leak will occur.

Example 1

#include <iostream>
#include <codecogs/units/date/date_str.h>

int main()
{
  char* pDateStr = Units::Date::date_str("l dS of F Y h:i:s A");
  std::cout<<pDateStr;
  delete[] pDateStr;
  return 0;
}

Output:

Tuesday 29th of March 2005 03:27:48 AM

Example 2

Any of the format string examples shown above can be used. Additionally a timestamp could be specified.

#include <iostream>
#include <codecogs/units/date/date_str.h>

int main()
{
  char* pDateStr = Units::Date::date_str("j, n, Y ", 2123011);
  std::cout<<pDateStr;

  delete[] pDateStr;
  return 0;
}

Output:

4, 7, 1100

Parameters

Format
The format string, see the format character tables above (DateString::fill).
Date
The timestamp to format as a serial Julian date (see date); defaults to -1, meaning use the current local time.

Returns

The formatted string
GPL Licence — free for non commercial use. See Licence details.