How to convert a bash string to date format? [duplicate] - linux

This question already has answers here:
How to convert YYYYMMDDHHMMSS to a date readable by `date`
(6 answers)
Closed 10 months ago.
I have a bash string 20220416124334 (the string is not epoch) and I want to convert it to date format so it should look like this: 2022/04/16 12:43:34
I've tried:
[manjaro#manjaro ~]$ date -d '20220416124334' "+%Y/%m/%d %H:%M:%S"
date: invalid date β€˜20220416124334’
How should I do it correctly ?

I would expect to use the date command, as you employ it, with a number that represents the number of seconds since the epoch. It seems your number is a formatted date yyyymmddhhmiss.
If you don't need to validate the string that represents a formatted date, then you can use sed to insert extra formatting characters:
echo '20220416124334' | sed -E 's/(....)(..)(..)(..)(..)(..)/\1\/\2\/\3 \4:\5:\6/'
If you end up taking as input a number of seconds since the epoch, then do it this way, keeping in mind that a number means different times in different time zones at different times of the year (eyeroll):
date -u -r 1650113014 "+%Y/%m/%d %H:%M:%S"

Related

Converting Dates - What format is 20130606T083000.093660900? [duplicate]

This question already has an answer here:
How to convert string format date yyyymmddThhmmss.sss to date in Bash?
(1 answer)
Closed 6 months ago.
Bit of an issue with date formatting:
Looking to format this date format " 20130606T083000.093660900 "
2013 06 06 T 08 30 00. 093660900
YYYY MM DD T HH MM SS NANOSECS
Formatting this into Epoch (which I believe is the second time given below without decimals) time would be great, I don't have much experience working with date time and changing them.
My goal is to realistically subtract
20130606T083000.093660900 FROM 1370507400093660900
Any help with even telling me what format the first date is, as it is apparently NOT ISO 8601, thanks!
You can use sed to convert the input to something GNU date can parse, then use GNU date to convert it to the format of the other date. Then compare:
din='20130606T083000.093660900'
dcmp='1370507400093660900'
dstr=$(sed 's/\(..\)\(..\)T\(..\)\(..\)/-\1-\2T\3:\4:/' <<<"$din")
timestamp=$(TZ=UTC0 date +%s%N --date="$dstr")
diff=$(( dcmp - timestamp ))
# diff is now: 0
I have forced timezone to avoid localisation concerns.

Sum number of days to specific date in dd/mm/yyyy format in bash

This might be a simple problem but I'm having some trouble getting trough it:
I need to sum a number of weeks to a specific date that is in the format dd/mm/yyyy.
I know I can sum number of weeks to the current day using date -d "+1 week" +"%d%m%Y", but I'm having trouble when I try to do this to a specific date in the format I mentioned above (29/06/2019, for example).
Would anyone be able to explain how I can perform this operation?
$ date -d '20190629+1 week' +"%d%m%Y"
06072019
$ date -d '06/29/2019+1 week' +"%d%m%Y"
06072019
without delimiters it's not a valid date format though.

Linux Shell Script - Date format not working [duplicate]

This question already has answers here:
GNU date and custom formats
(2 answers)
Closed 4 years ago.
I am getting date in following format from a system and I need to use it for calculating some time difference.
Input format - 30-MAR-18 01.40.04.812030 AM PST
I am using following code but I am getting format errors.
#!/usr/bin
start_string="30-MAR-18 01.40.04.812030 AM PST"
TZ=GMT date --date="$start_string" "+%s"
#Above line is just to convert into seconds and print
This throws the error "date: invalid date β€˜30-MAR-18 01.40.04.812030 AM PST’"
I am lost on how to convert this into date format and then use it.
All help appreciated.
This is different from question asked here because I am dealing with an input that has a "." and not a ":" as the time separator.
Give the format like this:
start_string="30-MAR-18 01:40:04.812030 AM PST"

Use variable within unix date command

OS: CentOs 6 Final 64 Bit
I'm trying to write a shell script which gets the date a given number of days ago. The number of days is dynamic and the code I'm trying to use is this...
date +%d-%m-%Y -d '$days days ago'
The response I get back is...
date: invalid date `$days days ago'
Can anyone suggest to me the correct format to code this as I'm pulling my hair out and soon my teeth!!
Cheers
Paul
Change single quote ' to double quote ". Otherwise the string $days will be sent instead of its value.
Try:
[matias#lappie ~]$ date +%d-%m-%Y -d "now - $days days"
04-02-2015

how to get day of the year in shell?

How can I get the day of the year in shell?
date '+%V' will give me the week of the year, which is 15 for today; but I need to find the day of the year!
From the coreutils date manual:
%j day of year (001..366)
Use the date command and the %j option...
doy=$(date +%j)
POSIX mandates the format string %j for getting the day of the year, so if you want it for today's date, you are done, and have a portable solution.
date +%j
For getting the day number of an arbitrary date, the situation is somewhat more complex.
On Linux, you will usually have GNU date, which lets you query for an arbitrary date with the -d option.
date -d '1970-04-01' +%j
The argument to -d can be a fairly free-form expression, including relative times like "3 weeks ago".
date -d "3 weeks ago" +%j
On BSD-like platforms, including MacOS, the mechanism for specifying a date to format is different. You can ask it to format a date with -j and specify the date as an argument (not an option), and optionally specify how the string argument should be parsed with -f.
date -j 04010000 +%j
displays the day number for April 1st 00:00.
The string argument to specify which date to examine is rather weird, and requires the minutes to be specified, and then optionally allows you to prefix with ((month,) day, and) hour, and optionally allows year as a suffix (sic).
date -j -f "%Y-%m-%d" 1970-04-01 +%j
uses -f format date to pass in a date in a more standard format, and prints the day number of that.
There's also the -v option which allows you to specify relative times.
date -j -v -3w +%j
displays the day number of the date three weeks ago.
If you are looking for a proper POSIX-portable solution for getting the day number of arbitrary dates, the least unattractive solution might be to create your own program. If you can rely on Python or Perl (or GNU Awk) to be installed, those make it relatively easy, though it's still a bit of a chore using only their default libraries.

Resources