Covert specific Time to different TIMEZONE in UNIX KSH script - shell

I want to convert specific/Input date to different timezone. I have tried below code.
dateYMD="2019/2/28 12:23:11.46"
timesydney=$(TZ=Australia/Sydney date -d "$dateYMD" +%s)
But above code give me a below error....
date: Not a recognized flag: d
Usage: date [-u] [+"Field Descriptors"]

Related

How to get Date in java.util.Date instead of Unix Timestamp in bash script?

I have an .sh script on my Linux server.
I need the date in milliseconds in Java, but everything I find on the net is giving me Unix Timestamp.
Like this: date=$(date -d 'today 00:00:00' "+%s")
I need java milliseconds, like here: https://www.fileformat.info/tip/java/date2millis.htm.
How do I get that easily without writing a long java code?
There has to be an easy solution.
It's just unix epoch in milliseconds instead of seconds, so simply replace +%s with +%s%3N.
epoch_milli=$( date -d 'today 00:00:00' +%s%3N )
$ date +%s; date +%s%3N
1666798614
1666798614357
Conversion using your site
Assuming your date supports this format
%N nanoseconds (000000000..999999999)
so you can do something like
date +%s.%N
to get nanoseconds, then you can do the math if you want millies.

Get first date of current month in MacOS terminal

uisng the command echo $(date "+%F" -d "$(date +'%Y-%m-01') 0 month") i can get the first date of that month. but the same is not working on mac terminal. What needs to be changed?
echo $(date "+%F" -d "$(date +'%Y-%m-01') 0 month")
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
The date libraries in *BSD and GNU systems are different and the options supported vary between them. The -d flag which supports a myriad of functions in GNU date is replaced by the -f option in BSD (See FreeBSD Manual Page for date)
For your requirement though, you need the time adjust flag using the -v flag. So the first day of the current month should be
date -v1d -v"$(date '+%m')"m '+%F'
The -v1d is a time adjust flag to move to the first day of the month
In -v"$(date '+%m')"m, we get the current month number using date '+%m' and use it to populate the month adjust field. So e.g. for Aug 2020, its set to -v8m
The '+%F' is to print the date in YYYY-MM-DD format. If its not supported in your date version, use +%Y-%m-%d explicitly.
Or if your requirement is to print the date for all the months
for mon in {1..12}; do
date -v1d -v"$mon"m '+%F'
done

bash: query timestamp of UTC date on BSD

What I intent to get is
$ xxx 2019-10-11 <= insert your command
1570752000
The output is timestamp in Oct 11 00:00:00 UTC 2019. I find a good way to do this in gnu, but not in bsd
This should work:
date -j -f '%F %T %Z' '2019-10-11 00:00:00 U' '+%s'
-j is for dry-run; i.e it prevents date from changing system date and time,
-f is for specifying input format,
+%s is for converting given date to seconds since Epoch.
On NetBSD the following will work:
TZ=GMT0 date -d '2019-10-11 00:00:00' '+%s'
Note the use of the TZ environment variable to specify the input timezone instead of trying to have it parsed from the input (though it may be possible to have a more properly formatted timezone parsed from the input, though then that leaves the question of what timezone the output should be formatted in).
On MacOS you might try something similar to what Oguz suggested:
TZ=GMT0 date -j -f '%Y-%m-%d %H:%M:%S' '2019-10-11 00:00:00' '+%s'

Commandline to convert ISO8601 time to unix timestamp

What is the most efficient way to get a unix timestamp for a given ISO 8601 date and vice versa?
There are several third party websites to do this. However I am looking for a simple command that can be executed at the Linux prompt
Convert ISO Date/Time to Unix timestamp
date -d 'date' +"%s"
Example
bash-# date -d 'Fri Dec 8 00:12:50 UTC 2017' +"%s"
bash-# 1512691970
man -a date
-d, --date=STRING
display time described by STRING, not 'now'
%s seconds since 1970-01-01 00:00:00 UTC
Convert Unix timestamp to ISO Date/Time
date -Iseconds -d #<unix timestamp>
Example
bash-# date -Iseconds -d #1512711426
bash-# 2017-12-07T21:37:06-0800
man -a date
-d, --date=STRING
display time described by STRING, not 'now'
-I[TIMESPEC], --iso-8601[=TIMESPEC]
output date/time in ISO 8601 format.
TIMESPEC='date' for date only (the default), 'hours',
'minutes', 'seconds', or 'ns' for date and time to the
indicated precision.

Converting date string to date object in BSD MacOSX

In mac osx bsd, date -vmon will convert the current date to Monday's date and date +%w displays day of week.
Say, I have input date 9/14/16 in the format mm/dd/yy. How can I convert this string pattern to a date object so that I could perform date functions like -vmon and +%w on it.
I tried the following: startDate=$(date -jf "%m/%d/%y" "9/14/16") and then tried to perform $startDate +"%w" but it doesn't work. I doubt that the startDate is not date and actually String.
How can I convert the string to date so that I perform date manipulation on it?
Edit: The requirement for doing this is: Say an input date is given. Then corresponding to that date, I want to get the beginning and ending working dates of the week i.e Monday's date and Friday's date. Then I want to get next week's Monday's date and Friday's date. How can I do this?
You need to use date twice, once to convert from the string input to an 'epoch time' (the number of seconds since The Epoch — aka 1970-01-01 00:00:00 +00:00), and once to convert an epoch time to an output format of your choosing:
rdate="9/14/16"
epoch_time=$(date -j -f '%m/%d/%y' "$rdate" +'%s')
date -j -vmon -r "$epoch_time"
date -j -vfri -r "$epoch_time" +'%w'
That gave me the output:
Mon Sep 12 10:14:44 PDT 2016
5
Note that Friday is day 5 of the week, so the output is correct. The use of %w when you specify -vdow seems a little moot; you know that the output will be 5. But you can use any other format characters that you need too.
Note that the time portion is defaulted to the current time. You could add 00:00:00 to the converted date and %H:%M:%S to the conversion format (-f argument) to work with midnight, etc.
You can combine it into one command line:
$ date -j -vmon -r $(date -j -f '%m/%d/%y %H:%M:%S' "$rdate 00:00:00" +'%s') +'%Y-%m-%d %H:%M:%S %w %s'
2016-09-12 00:00:00 1 1473663600
$
The last example on the man page for date on Mac OS X mentions the %s format specifier, which is the key to getting this to work. Well, that and using date twice.
Note that the man page gives synopses:
date [-ju] [-r seconds] [-v [+|-]val[ymwdHMS]] ... [+output_fmt]
date [-jnu] [[[mm]dd]HH]MM[[cc]yy][.ss]
date [-jnu] -f input_fmt new_date [+output_fmt]
date [-d dst] [-t minutes_west]
The 'usage' message is less helpful:
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
The usage message doesn't show that you can't use -f with -r or -v, whereas the manual is clear that you cannot.
Tested: Mac OS X 10.11.6 El Capitan

Resources