LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-07-2007, 09:29 PM   #1
monu
LQ Newbie
 
Registered: Jun 2007
Posts: 17

Rep: Reputation: 0
date format


Hi,

I am having problem with creatng date formate: YYYYMMDD .

My code is: date +%Y%m%d but its not working it. I checked %Y only displays current year not past year. suppose if i enter my date: 18240804 it doesn't accept it. IT only takes 20070804.. not past years. What is date -s do?

Thanks
 
Old 10-07-2007, 09:49 PM   #2
monu
LQ Newbie
 
Registered: Jun 2007
Posts: 17

Original Poster
Rep: Reputation: 0
for example:


#!/bin/bash

c_date=`date +%Y`

read -p echo "enter year" year

if......
echo "working"
else

echo "not working"

fi


enter year:2005
not working
enter year: 2007

working!

So my point is %Y is only for current year not for past year. I want to create script which accepts past years day and months like this: YYYMMDD. I read man page i couldn't find out what is date -s ? please provide me some example and with past year.

Thanks
 
Old 10-08-2007, 01:20 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
From the man page:
date - print or set the system date and time
I don't think it does what you want ie check random strings for validate dates (if that's what you meant).
 
Old 10-08-2007, 02:32 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
The GNU date command accept the -d option to specify a date other than the current date. From the info page:
Quote:
`-d DATESTR'
`--date=DATESTR'
Display the date and time specified in DATESTR instead of the
current date and time. DATESTR can be in almost any common
format. It can contain month names, time zones, `am' and `pm',
`yesterday', etc. For example, `--date="2004-02-27
14:19:13.489392193 +0530"' specifies the instant of time that is
489,392,193 nanoseconds after February 27, 2004 at 2:19:13 PM in a
time zone that is 5 hours and 30 minutes east of UTC. *Note Date
input formats::.
Check the section "Note Date input formats" for details on how to specify the past date. The following are all working examples:
Code:
date -d "23 June 1867" +%Y%m%d
date -d "Jul 11, 1968 - 3 days" +%Y%m%d
date -d "2 days ago" +%Y%m%d
and so on.

Last edited by colucix; 10-08-2007 at 02:35 AM.
 
Old 10-08-2007, 03:48 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
I'm afraid that date won't handle dates before 1902

Code:
$ date -d 1902-04-08
Tue Apr  8 00:00:00 GMT 1902

$ date -d 1901-04-08
date: invalid date `1901-04-08'
Not that I can see any useful application to that,
mind you....


Cheers,
Tink
 
Old 10-08-2007, 04:26 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by Tinkster View Post
I'm afraid that date won't handle dates before 1902
Uh... mine seems to work...
Code:
$ date -d 1902-04-08
Tue Apr  8 00:00:00 CET 1902

$ date -d 1901-04-08
Mon Apr  8 00:00:00 CET 1901
I have date version:
Code:
date (GNU coreutils) 5.97
I will investigate!
Cheers
 
Old 10-08-2007, 10:47 AM   #7
monu
LQ Newbie
 
Registered: Jun 2007
Posts: 17

Original Poster
Rep: Reputation: 0
hi

Thanks

I will try that
 
  


Reply

Tags
bash, shell


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
'date' command format Neorio Linux - General 3 01-18-2011 05:18 AM
Date format Bill Jones Linux - Newbie 1 12-23-2006 11:07 AM
date format kalleanka LQ Suggestions & Feedback 9 11-15-2006 11:36 AM
Regarding date Format ancys Programming 4 08-10-2006 05:35 AM
Date format Mik LQ Suggestions & Feedback 8 12-05-2004 10:14 AM


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

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