convert date variable to doy variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert date variable to doy variable
# 1  
Old 12-16-2008
convert date variable to doy variable

Hello:

I have a script that requires the use of DOY (as a variable) instead of YY/MM/DD. I already obtained these parameters from downloaded files and stored them as variables (i.e. $day, $month, $year).

Is there any simple script that I could incorporate in mine to do this job?

Regards,

Antonio
# 2  
Old 12-16-2008
Code:
date '+%j'

# 3  
Old 12-16-2008
To convert a string to date and output the "Julian" date use the following:

Code:
 # date -d "2008-01-02" +%j
002

This is assuming your using GNU date.
# 4  
Old 12-16-2008
Just one more question. I was able to get the DOY. I included it in my script by making the following:

$comm = "date -d $year-$month-$day +%j";
system("$comm");

For 2008/12/16 the scripts prints in the screen 351.

However, at the end of the day I need to capture the answer, 351 for instance, as a variable. I am sorry but I am new at linux.

Regards,
# 5  
Old 12-16-2008
Shell:
Code:
DOY=`date -d $year-$month-$day +%j`

Perl:
Code:
$DOY=`date -d $year-$month-$day +%j`;


Last edited by Ikon; 12-16-2008 at 05:35 PM..
# 6  
Old 12-16-2008
Is this a Perl script? Sounds like it if you are using the system command.

Code:
$ cat ./date_test.pl
#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw(strftime);
use Time::Local;

#If you just want the julian of the current system date
my $x;

$x = strftime("%j", localtime());

print "x = $x\n";

#If you want the julian of a specific date, let's say 2008-12-10
my $testdate;
my $yyyy = 2008;
my $mm   = 12;
my $dd   = 10;

$testdate = timelocal(0, 0, 0, $dd, $mm-1, $yyyy);
$x = strftime("%j", localtime($testdate));
print "x = $x\n";

exit 0;


$ ./date_test.pl
x = 351
x = 345

$ date -d "2008-12-16" +%j
351

$ date -d "2008-12-10" +%j
345

# 7  
Old 12-17-2008
If you are using ksh93, support for date calculations and conversions is built in.
Code:
$ doy=$(printf "%(%j)T" "2008-12-16")
$ print $doy
351
$ doy=$(printf "%(%j)T")
$ print $doy
352
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. Shell Programming and Scripting

Convert a date stored in a variable to epoch date

I am not able to pass date stored in a variable as an argument to date command. I get current date value for from_date and to_date #!/usr/bin/ksh set -x for s in server ; do ssh -T $s <<-EOF from_date="12-Jan-2015 12:02:09" to_date="24-Jan-2015 13:02:09" echo \$from_date echo... (7 Replies)
Discussion started by: raj48
7 Replies

3. UNIX for Advanced & Expert Users

Convert variable blocked file

Dear All, I am receiving Fixed blocked file from AS/400 system to my unix box. Could you please advise us how to convert this fix blocked file to variable blocked file. Do we have any command in Unixt or program. I am new to this Unix system Thanks & Regards, Colam (1 Reply)
Discussion started by: coolmaninit
1 Replies

4. Shell Programming and Scripting

How to convert string(variable) into date( epoch) in ksh on HPUX machine?

Hi all, I have used a bash script which ultimately converts a string into date using date --date option: DATE=$DATE" "$TIME" "`date +%Y` //concatenating 2 strings TMRW_DATE=`date --date="$DATE" +"%s"` //applying date command on string and getting the unixtime Please use code tags... (7 Replies)
Discussion started by: Rashu123
7 Replies

5. Shell Programming and Scripting

Read Variable From Fille And Convert it to Integer

I read 3 variables from from Inputfile.txt the third one "startnumber" is a number when i compare it with 9 ($startnumber -le 9) it give's me a "unary operator expected", i know that -le is for number comparison. What i need is to convert $startnumber to integer (i have try to do it with expr but... (8 Replies)
Discussion started by: marios
8 Replies

6. Shell Programming and Scripting

convert hex to binary and send to variable

Folks, can anyone help with a script to convert hex to binary digits, and break the 32 bit binary into packs of 4 and send them to 8 different variables.Any help is sincerely appreciated. Thanks venu Its in korn shell...... (24 Replies)
Discussion started by: venu
24 Replies

7. Shell Programming and Scripting

Convert value stored in a variable to epoch time?

Hello I have a the creation date of a file stored in a variable in the following format: Wed May 06 10:14:58 2009Is there a way I can echo the variable and display it in epoch time? I've done a lot of searching on this topic, but haven't managed to get a solution. I'm on Solaris 10. ... (2 Replies)
Discussion started by: Glyn_Mo
2 Replies

8. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

9. Shell Programming and Scripting

convert variable content to array

Hi All, I have a variable in a shell script which holds let say n paarmeteres with space separate them : $var = par1 par2 par3 par4 parn; so if I print this variable this is what I'll see: par1 par2 par3 par4 parn I need to insert each parameter to an array , so I can go over on each... (3 Replies)
Discussion started by: Alalush
3 Replies

10. Shell Programming and Scripting

How do you take what a user types in and convert it into a variable to be used

I am a newbie to programming on the unix Command Line*, and was wondering how you take what a user types in and convert it into a variable to be used in the script? *I always used it for file editing, but nothing more. Then I began to use BBedit. :confused: (1 Reply)
Discussion started by: signebedi
1 Replies
Login or Register to Ask a Question