How do I convert the __DATE__ macro to a string?

This is a discussion on How do I convert the __DATE__ macro to a string? within the C++ Programming forums, part of the General Programming Boards category; Hello, I was wondering how to convert the __DATE__ macro to a C++ string? Thanks to any one who replies ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    How do I convert the __DATE__ macro to a string?

    Hello,
    I was wondering how to convert the __DATE__ macro to a C++ string?

    Thanks to any one who replies in advance.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    You mean the obvious way doesn't work?
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string foo = __DATE__;
      std::cout << foo << std::endl;
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Salem View Post
    You mean the obvious way doesn't work?
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string foo = __DATE__;
      std::cout << foo << std::endl;
      return 0;
    }
    Yep, the obvious way does not work in my compiler.
    I get a compiler error that says

    error: expected primary-expression before ';' token
    at the assign line, though before reading your post, I had tried using the string += operator and it produced the above error. Then, after reading your post, I changed the code to assign a new string the __DATE__ macro, then add the new string to the output string using the += operator, but it still produced the same error at the assign line as it did at the append line.

    EDIT: Nevermind...it turned out I had written #define __DATE__ at the top of the file. Once I removed that, it compiled no problems.
    Last edited by Programmer_P; 01-17-2011 at 01:03 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21