Bash Script Convert Julian Date To A Calendar Date

Use this tool to convert between date and Julian (yyddd) date formats. Enter a valid date or Julian date, and click 'Convert'. To see an example, click 'Show Example'. Enter dates in the form mm/dd/yy, dd-mmm-yyyy, 'today', '+1 day' or similar. Enter Julian dates in the format yyddd, yy-ddd or yy.ddd. Remember: This tool can only convert date. To compare two dates in bash, you can use the unix timestamp value of both date using the date command line or the printf method and a bash if statement with a bash arithmetic expansion, or by using a lexicographical comparison of the two dates string by using the double square bracket conditional construct. Third Tab of Date Calculator3.

In this article you’ll learn, how to convert a Julian date to a calendar date.

What is Julian date?

Julian date calendar was introduced by Julius Caesar. The length of Julian date is 5, in which first 2 digits represt the year and last 3 digits represent the day of the year.

For example:-

  • 1st January 2008, Julian date 08001. Where “08” being the year and “001” being the date

To convert the Julian date format to a calendar date, we use IF, LEFT and MOD function in Microsoft Excel.

Let’s take an example,

In Column A we have Julian date and in column B we want to convert Julian date into calendar date.

Follow below given steps:-

  • Enter the formula in cell B2
  • =('1/1/'&(IF(LEFT(A2,2)*1<20,2000,1900)+LEFT(A2,2)))+MOD(A2,1000)-1
  • Press Enter
  • The function will convert the Julian date format in to Calendar date
  • Copy the same formula by pressing the key Ctrl+C and paste in the range B3:B10 by pressing the key Ctrl+V on your keyboard

Formula Explanation:-

  • (IF(LEFT(A2,2)*1<20,2000,1900)+LEFT(A2,2))) :- This part of formula will help to get the year
  • MOD(A2,1000) :- This part of formula will help to get the number of days
  • By using rest part formula will calculate the date as number, because excel has stored the date as numbers.

In this way, we can convert the Julian date to a calendar date in Microsoft Excel.

Important Note:- To convert the 4 digit’s Julian date into calendar date we have to convert Julian date into text format.

Take a look at the below example,

If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook.

We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write us at info@exceltip.com


Bash uses `date` command to display or change the current date and time value of the system. Date and time value can be printed in different formats by using this command. This command can also be used for calculating date and time value related tasks. `date` command without any option just prints the current system’s date and time value. This command has many formatting options to format the output. The uses of these options are shown in this tutorial by using various examples.Bash script convert julian date to a calendar date

Syntax:

date[option]... [+Format]
date[option][MMDDhhmm [[CC] YY][.ss]]

Bash Script Convert Julian Date To A Calendar Date Today

Different format codes or characters can be used with the date options to generate the formatted output. Some common options and format types are mentioned below.

Options:

-d or –date=StringIt displays the time set by the String value.
-s, –set=StringIt sets the time set by the String value.
-f or –file=DateFileIt is used to process multiple dates.
-I or –iso-8601[=Timespec]It is used to create an ISO 8601 compliant date/time string output.
-r or –reference=FileIt is used to display the last modification time of a file.
-u, –utc, –universalIt is used to display or set Coordinated Universal Time.
–helpIt is used for getting the help of this command.
–versionIt is used to get version information.

Some Format codes:

%aPrint weekday names in short form (e.g., Sun)
%APrint full weekday names (e.g., Sunday)
%bPrint month name in short form (e.g., Jan)
%BPrint full month name (e.g., January)
%cPrint date and time (e.g., Mon Mar 11 23:05:25 2019)
%CPrint century; like %Y, except omit last two digits (e.g., 25)
%dPrint day of the month (e.g, 01)
%YPrint 4 digits year (e.g. 2019)
%yPrint 2 digits year (e.g. 19)
%DPrint date; same as %m/%d/%y
%ePrint day of the month, same as, %d
%FPrint full date; same as, %Y-%m-%d

Example-1: Display date in the particular format

By default `date` displays the current date and time value. Each part of date and time values can be printed separately by using different data options. The following command will print the date value only.

Output:

Here, ‘%d’ is used for printing day value, ‘%B’ is used for printing full month name and ‘%Y’ is used for printing full year value.

Example-2: Change the current date and time

The current date and time value can be changed by using -s option. You must have root privilege to change the system’s date and time. The following command will change the current date to ‘03/17/2019’ and time to ‘03:30:00‘.

Output:

Example-3: Find the particular date and time using days

Sometimes we need to find out the future or previous date and time. Any date can find out by using `date` command and defining days, month and year values in –date option. The following commands will calculate the date and time after 15 days and before 15 days.

Output:

Example-4: Find the particular date and time using times

Like the previous example, future or previous times can be calculated by using `date` command. The following commands will calculate the future time after ‘5 hours 20 minutes’ of current times and before 2 hours and 20 minutes of current times.

$ date
$ date--date='5 hours 20 minutes'
$ date
$ date--date='-2 hours -20 minutes'

Output:

Convert Julian Date To Calendar Date In Unix

Example-5: Convert current date and time to UNIX epoch time

According to UNIX epoch time, the time value is calculated in seconds from the date, 1st January 1971. This time value can be used to calculate the time difference. `date` command can be used to convert any date value to UNIX epoch time. The following command will convert the current system date and time to UNIX epoch time.

Output:

Example-6: Convert UNIX epoch time to date and time

Using `date` command anyone can convert any UNIX epoch time to date and time value. The following command converts ‘1552298500′ epoch value to its corresponding date and time value.

Output:

Example-7: Find out the weekday based on date

`date` command can be used to find out the weekday name, month name or year value from any date value. The following command will find the weekday name of 1st January 2019 and the output is ‘Tuesday’.

Output:

Example-8: Using date command in a bash script

A bash file named timediff.sh is created with the following code. In this script, two date values are taken from command line arguments which are stored on $1 and $2. $START variable stored the UNIX epoch time value of $1 and $END variable stored the UNIX epoch time value of $2. The difference between these two values is calculated and stored to $diff variable in seconds. Next, the seconds are converted to days and printed.

Bash Script Convert Julian Date To A Calendar Date Calendar

timediff.sh

#!/bin/bash
START=`date-d$1 +%s`
END=`date-d$2 +%s`
((diff=$END-$START))
((days=$diff/(60*60*24)))
echo'The time escaped = $days days'

The script is executed with two date values as command line arguments. Here, 2019-01-01 and 2020-01-01 dates are used and the difference between these two dates is 365 days.

Conclusion

You can use `date` command for various purposes in the bash script. Some uses of date values are explained in this tutorial with the above examples. You can also use this command to separate the parts of time value by using different options and formats. Hope, this tutorial will help the readers to understand the use of `date` command and apply them properly.