I want to get this time in milliseconds
01/Mar/2012:09:08:00
I thought doing the following would store the new date then I can convert the date into milliseconds
time=01/Mar/2012:09:08:00
newDate=date --set="$time";
What do I need to do to get this working?
You should convert '01/Mar/2012:09:08:00' to a valid date string '01 Mar 2012 09:08:00'
$ time=01/Mar/2012:09:08:00
$ time="${time//// }"
$ time="${time/:/ }"
$ newDate=`date -d "$time" +%s000`
$ echo $newDate
1330564080000
Use Timer() method to get time then put that time in formatnumber() method as like
FormatNumber(Timer(), 2)
I have the easiest way to convert date into milliseconds:
echo $(date +'%s')
Related
I have problem that is using custom date format like date +%y%j.%H%M%S,
And what i want is to add 15 mins on the this date on just on current date of system. so that i can use for further calculation into my process.
I have tried with below code -
$uprBond=`date +%y%j.%H%M%S`
$ echo $uprBond
16079.031135
$ date -d "$(uprBond) + 5 minutes" +%y%j.%H%M%S
op > bash: uprBond: command not found
16079.035920
I am failing while passing the above date format , Can anybody please help on this.
Just for note, below is the piece of code is working when i used date function instead of defined date variable i.e. $uprBond (I don't want to use predefined date because we have some old same formatted date which needs that adding of mins).
date +%y%j.%H%M%S -d "`date` + 5 minutes";
op > 16079.040724
With GNU date, GNU bash 4 and its Parameter Expansion:
#!/bin/bash
uprBond="$(date +%y%j.%H%M%S)"
year="20${uprBond:0:2}"
doy="${uprBond#${uprBond:0:2}}"
doy="${doy%.*}"
time="${uprBond#*.}"
time="${time:0:2}:${time:2:2}:${time:4:2}"
in5min=$(date -d "${year}-01-01 +${doy} days -1 day +5 minutes ${time}" "+%y%j.%H%M%S")
echo "now: $uprBond"
echo "in 5min: $in5min"
Output:
now: 16079.145026
in 5min: 16079.145526
I'm trying to make some calculations with date in bash script but can't find out the right syntax. I get a string from a file that I convert to a date. Then I want to get the date for one and two days ahead. Looking on another StackOverflow posts it looked easy adding days to today date. This is what I am doing now:
# Extract date string from file
ctldate=`awk 'NR==8 { print $4 }' a-AC-2015-02-10-120000-g3.ctl`
echo $ctldate
12:00Z10feb2015
# Convert string to date
ctldate2=`date +'%d/%m/%Y' -d $ctldate`
echo $ctldate2
10/02/2015
# Try to add a day, should be 11/02
data1=$(date +'%d/%m/%Y' -d "$ctldate" --date='1 day')
echo $data1
12/02/2015
# Also tried
data1=$(date +'%d/%m/%Y' -d "$ctldate2" --date='1 day')
echo $data1
12/02/2015
# And
data1=`date +'%d/%m/%Y' -d $ctldate --date='1 day'`
echo $data1
12/02/2015
It seems that I'm not properly passing $ctldate var to command and that the base date for calculation is today.
Thanks in advance
When you pass several -d or --date, date uses the last one. So when you do:
date -d "$ctldate" --date='1 day'
date will take into account --date='1 day' as it's the last -d/--date argument, and will happily show you tomorrow's date.
Instead you should use:
date -d "$ctldate +1 day"
I have a date in format 'YYYYMMDDHHMMSS' and I need to convert it to Unix timestamp.
I tried to date -d '20140826225834' but I get 'invalid date' error. I asume that I would have to convert what I have ( 20140826225834 ) to accepted date and then convert it to timestamp?
Edit: I have sed this date from 2014-08-21_23.03.07 - maybe it would be easier to convert this date type
You should probably change the format of the date you get, so that date can handle it. I change it to a YYYY/MM/DD HH:MM:SS format with sed.
$ date -d"$(sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834")" "+%s"
1409086714
By pieces:
$ sed -r 's#(.{4})(.{2})(.{2})(.{2})(.{2})#\1/\2/\3 \4:\5:#' <<< "20140826225834"
2014/08/26 22:58:34
$ date -d"2014/08/26 22:58:34"
Tue Aug 26 22:58:34 CEST 2014
$ date -d"2014/08/26 22:58:34" "+%s"
1409086714
You could use PHP, since PHP's strtotime() can parse your input format:
#!/bin/bash
input="20140826225834"
output=$(php -r 'echo strtotime("'"$input"'");')
echo "$output" # 1409086714
I am mediating some data from one file-format to another and am using bash to perform the mediation steps. Part of the process involves taking a timestamp from the filename to create a starttime and then adding 5 minutes to that to create the endtime. The filename gives me a date in this format YYYYmmddHHMMSS. I can easily add 500 to each starttime to get an endtime that works for all starttimes except 55 mins past the hour but this obviously not correct so I have tried to use the date --date= code to add the 5minutes like this:
date --date='$starttime +5 minutes' '+%Y%m%d%H%M%S'
for example:
date --date='20140220125500 +5 minutes' '+%Y%m%d%H%M%S'
However I get an error like this every time:
date: invalid date `20140220125500 +5 minutes'
Can anyone suggest what I need to change in my date statement or if there is a similar alternative syntax available?
This worked to me:
$ date --date='20140220 12:55:00 CET +5 minutes' '+%Y%m%d%H%M%S'
20140220130000
That is, the problem is that YYYYYMMDDHHMMSS alone is not understandable for date. Instead, give it on the YYYYYMMDD HH:MM:SS format.
As you are receiving the data on that YYYYYMMDDHHMMSS format, you can parse it with sed as follows:
$ file="YYYYmmddHHMMSS"
$ new_name=$(sed -e 's/^\(.\{8\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1 \2:\3:\4/' <<< "$file")
$ echo "$new_name"
YYYYmmdd HH:MM:SS
So in your case:
$ file="20140220125500"
$ new_name=$(sed -e 's/^\(.\{8\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1 \2:\3:\4/' <<< "$file")
$ echo "$new_name"
20140220 12:55:00
$ date --date="$new_name CET + 5 minutes"
Thu Feb 20 13:00:00 CET 2014
Note I used
$ date --date="$new_name CET +5 minutes" '+%Y%m%d%H%M%S'
^ ^
note the double quotes to have the var expanded
In a bash script, if I have a number that represents a time, in the form hhmmss (or hmmss), what is the best way of subtracting 10 minutes?
ie, 90000 -> 85000
This is a bit tricky. Date can do general manipulations, i.e. you can do:
date --date '-10 min'
Specifying hour-min-seconds (using UTC because otherwise it seems to assume PM):
date --date '11:45:30 UTC -10 min'
To split your date string, the only way I can think of is substring expansion:
a=114530
date --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
And if you want to just get back hhmmss:
date +%H%M%S --date "${a:0:2}:${a:2:2}:${a:4:2} UTC -10 min"
why not just use epoch time and then take 600 off of it?
$ echo "`date +%s` - 600"| bc; date
1284050588
Thu Sep 9 11:53:08 CDT 2010
$ date -d '1970-01-01 UTC 1284050588 seconds' +"%Y-%m-%d %T %z"
2010-09-09 11:43:08 -0500
Since you have a 5 or 6 digit number, you have to pad it before doing string manipulation:
$ t=90100
$ while [ ${#t} -lt 6 ]; do t=0$t; done
$ echo $t
090100
$ date +%H%M%S --utc -d"today ${t:0:2}:${t:2:2}:${t:4:2} UTC - 10 minutes"
085100
Note both --utc and UTC are required to make sure the system's timezone doesn't affect the results.
For math within bash (i.e. $(( and ((), leading zeros will cause the number to be interpreted as octal. However, your data is more string-like (with a special format) than number-like, anyway. I've used a while loop above because it sounds like you're treating it as a number and thus might get 100 for 12:01 am.
My version of bash doesn't support -d or --date as used above. However, assuming a correctly 0-padded input, this does work
$ input_time=130503 # meaning "1:05:03 PM"
# next line calculates epoch seconds for today's date at stated time
$ epoch_seconds=$(date -jf '%H%M%S' $input_time '+%s')
# the 600 matches the OP's "subtract 10 minutes" spec. Note: Still relative to "today"
$ calculated_seconds=$(( epoch_seconds - 600 )) # bc would work here but $((...)) is builtin
# +%H%M%S formats the result same as input, but you can do what you like here
$ echo $(date -r $calculated_seconds '+%H%M%S')
# output is 125503: Note that the hour rolled back as expected.
For MacOS users you can do the following:
$(date -v -10M +"%H:%M:%S")
Date time without a specific format:
$(date -v -10M)
For non-macOS users:
Date time without a specific format:
date --date '-10 min'