Thread: How do I convert the __DATE__ macro to a string?

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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 02: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, 02:11 AM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03: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