Changing date using bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing date using bash script
# 1  
Old 09-11-2018
Changing date using bash script

I am trying to change dates in a bash script.

I have a start time and an endtime and want to increment the times. Basically
the month and day have to be incremented in a loop to create two strings, stm and
etm defining the start and end times.

Code:
stm="2014-05-13T00:00:00"
etm="2014-05-14T00:00:00"

------ Post updated at 06:33 AM ------

I have started doing this bash details for constructing the start and end strings.

Code:
# Covering months May to December 
for m in {5..12}; do
  mth=$((mth+1))
  echo "mth: $mth"
  for dy in {1..30}; do
    day=$((day+1))
    echo "day: $day"
  done
  echo ""
done

# Covering months Jan to April 
for m in {1..4}; do
  mth=$((mth+1))
  echo "mth: $mth"
  for dy in {1..30}; do
    day=$((day+1))
    echo "day: $day"
  done
  echo ""
done

# 2  
Old 09-11-2018
Welcome novilatte,

Sadly I don't know what you are trying to achieve as output here. Can you explain?

Is it homework perhaps? There is a special forum for this.

I could guess that you want a sequence of dates where the month and the day add one each iteration but I'm not sure why.

.... or is it that you want every combination of valid dates for these months. In this case, you are missing the 31st when that occurs and including the 30th February, and taking no account of a leap year.

Would the output from cal be useful?
Can you explain the purpose of this in context? There may be a better way.




Kind regards,
Robin
# 3  
Old 09-11-2018
Hi,
Just an example with command linux date:
Code:
$ for i in 1 2 3 4 5 60 3600 86400
> do
> date --date "2014-05-14T00:00:00 $i seconds" "+%Y-%m-%dT%H:%M:%S"
> done
2014-05-14T00:00:01
2014-05-14T00:00:02
2014-05-14T00:00:03
2014-05-14T00:00:04
2014-05-14T00:00:05
2014-05-14T00:01:00
2014-05-14T01:00:00
2014-05-15T00:00:00

Here, I use "seconds", but I would use "minutes", "hours", "days",...

Regards.
# 4  
Old 09-11-2018
I am writing a data request for transfer oe seismological data from a webservice.

------ Post updated at 11:53 AM ------

I shoud be ok with specifying invalid dates (e.g., for february) as then the request will just send me an error code, and not get that data.
# 5  
Old 09-12-2018
Quote:
Originally Posted by novilatte
I am writing a data request for transfer oe seismological data from a webservice.

------ Post updated at 11:53 AM ------

I shoud be ok with specifying invalid dates (e.g., for february) as then the request will just send me an error code, and not get that data.
This is all interesting, but it still doesn't tell us what you are trying to do nor what output you are trying to produce. Robin asked you several questions in post #2 in this thread that you have not answered.

In what way does producing a list of months 2 through 13 on days 2 through 31 help you define two strings referencing a start time and an end time for an unspecified event.

Knowing that you don't mind creating invalid dates and don't mind losing some of your seismological data is interesting, but it still doesn't tell us what output you're trying to produce nor what that output represents in the real world.

We want to help you reach your goal, but if you don't explain what you're trying to do we are left guessing at what you might be trying to do and are highly unlikely to make a guess that will be of any use to you. Please help us help you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Changing CSV files with date . Subtracting date by values

Hi All, I have a CSV file which is as below. Basically I need to take the year column in it and find if the year is >= 20152 . If that is then I should subtract all values by 6. In the below example in description I am having number mentioned as YYWW so I need to subtract those by -5. Whereever... (8 Replies)
Discussion started by: arunkumar_mca
8 Replies

2. UNIX for Beginners Questions & Answers

Changing date format with script

I'm trying to change date format using this script from day/month/year to month/day/year #!/bin/bash while read line; do echo "$line" date=$(echo "$line" | cut -d/ -f1 ) month=$(echo "$line" | cut -d/ -f2 ) echo $month"/"$date"/2017" done < ~/Downloads/Dates.csv But I get output as... (5 Replies)
Discussion started by: sharat
5 Replies

3. UNIX for Beginners Questions & Answers

Connecting and changing variables in Bash script

#!/bin/bash X=$(</home/cogiz/computerhand.txt) # (3S 8C 2H 6D QC 8S 4H 5H) Y=$(</home/cogiz/topcardinplay.txt) # KS A=( "${Y::1}" ) B=( "${Y:1}" ) for e in ${X}; do if ]; then # searching for valid cards K,S or 8 ... (0 Replies)
Discussion started by: cogiz
0 Replies

4. Shell Programming and Scripting

Changing script from csh to bash

Hello Guys I have a script working fine on csh, but I would like to change it to bash, how I should change this command to be able to work as bash script. :wall: if ( $fsw > "0" ) then foreach swath ( `awk 'BEGIN {for (i='$fsw';i<='$lsw';i++) printf ("%s\n", i) }'` ) ## work to be done... (2 Replies)
Discussion started by: jiam912
2 Replies

5. UNIX for Dummies Questions & Answers

Date format in Bash Script

Hi Experts, We get "Day" of a month in a variable, so how to make date of out it? To make more sense if my variable $DAY contains "12" and month and year will be current date (as of today) so I want to see as output as 2013-09-12. How can I achive this bash script?? Any help is... (4 Replies)
Discussion started by: parpaa
4 Replies

6. Shell Programming and Scripting

Changing the date format in a script

Hi, I run a script which outputs various records, anyway one of the columns contains the date in the format DDMMYYYY, I would like to make this DDMMYY. Is there an easy way to do this, (11 Replies)
Discussion started by: mcclunyboy
11 Replies

7. Shell Programming and Scripting

changing cron using bash script

How can I change the cron entries only for ABC and XYZ from dosomething_1.0.sh to nowchanged_2.0 using a bash script ? Any help will be appreciated. # # ABC 00,05,10,15,20,25,30,35,40,45,50,55 * * * * /mydir/dosomething_1.0.sh 1>/dev/null 2>&1 # # ## # DEF... (4 Replies)
Discussion started by: jville
4 Replies

8. Shell Programming and Scripting

Changing File Time Stamp (Bash Script)

I need some help recovering from a "slight" screwup. We just moved 3 TB of data from one RAID Array to another. Low lever archive files. This was done with a regular cp (for some reason) and now we have lost all the timestamps on the files, and we urgently need to get the timestamps back on these... (7 Replies)
Discussion started by: chj
7 Replies

9. Shell Programming and Scripting

Bash script: issue changing directories in script

I am working on a script that checks two arguments at the command line. The first argument is a search pattern, the second can be a file or a directory, if it is a file a second script is called that checks it for the search pattern. If the second argument is a directory, it checks for the search... (5 Replies)
Discussion started by: Breakology
5 Replies

10. UNIX for Dummies Questions & Answers

Changing Creation Date to a Prespecified Date of a File In Unix

Dear Expert, Is there a command to do that in Unix? In such a way that we don't need to actually "write" or modified the content. -- monkfan (4 Replies)
Discussion started by: monkfan
4 Replies
Login or Register to Ask a Question