Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-10-2011, 06:41 PM   #1
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
How to print out "n" days ago's year, month and day?

Interesting uh?
For example, 2 days ago's date is 2011 Feb 8th.
This is not a homework...

Old 02-10-2011, 07:51 PM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
date -d '2 days ago'
Kevin Barry
2 members found this post helpful.
Old 02-10-2011, 08:22 PM   #3
Nominal Animal
Senior Member
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947Reputation: 947
I was just going to write what ta0kira wrote. The GNU date command is awesome. Anyway, in C, you can do
#include <stdio.h>
#include <time.h>

int main(void)
    time_t     now, then;
    struct tm *split;

    /* Get the current time */
    now = time(NULL);

    /* Convert the time to broken-down format */
    split = localtime(&now);

    /* Substract two days -- it is okay to go out of valid range */
    split->tm_mday -= 2;

    /* Renormalize the broken-down format, and return the time then. */
    then = mktime(split);
    printf("Two days ago was %04d-%02d-%02d, or %s\n",
           split->tm_year + 1900, split->tm_mon + 1, split->tm_mday,

    return 0;
This uses the local time as a basis. If you need UTC, use gmtime instead of locatime.

The call to mktime both normalizes the split date, and returns the resulting time in the base time_t format, seconds since epoch. See the ctime, localtime, and mktime man page for details. This is in the POSIX standard, and should work as-is on all sane operating systems.

It shouldn't be too difficult to add parameter parsing to the above, to get exactly what you need.
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 09:33 AM.
1 members found this post helpful.
Old 02-10-2011, 09:33 PM   #4
Registered: Apr 2010
Posts: 228

Rep: Reputation: 46
$ ruby -r'date' -e 'print ( - 48/24).strftime("%Y/%m/%d")'


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Perl: Condition?print "Is true":print "Is false"; angel115 Programming 2 09-17-2010 07:28 AM
how to get the first and the last day in the month with "date" command myhnet Linux - Newbie 9 06-16-2009 04:16 AM
LQ-er of the day/week/month/year? alan_ri LQ Suggestions & Feedback 0 01-24-2009 06:11 PM
Multicount in PHP , displays visits on current day, week, month, year and total visit xbaez Programming 1 04-24-2005 03:50 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration