Sponsored Content
Top Forums Shell Programming and Scripting Day/Hour diff between two date Post 302951222 by sea on Tuesday 4th of August 2015 05:18:33 AM
Old 08-04-2015
I'm no expert with awk, but here's something i'd like to share:

While i do agree that one-liners are cool, there are occasions it just makes no sense.
Saying, a well formated code is easier to read - and find issues/improvements, than within a one liner of that length. (me have FHD (1920x1080) display, and i need to scroll sideways)

This said, you might want to use a function (rather than an if-block) to transform time and date to seconds and to avoid things like:
Code:
... +((0*3600+0*60+0)+ ..

For which you could have just written +0.

Once you transformed the date+time to seconds, you simply subtract the one from the other and get the difference.

Hope my 2 cents help
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get date and time for past 1 hour from current date

Hi, I need to get the date and time for past 1 hour from the current date. Anyone know how to do so? Thanks (5 Replies)
Discussion started by: spch2o
5 Replies

2. Shell Programming and Scripting

Unix Script for getting date and validating just Hour

Hi, Can someone guide me to write a unix script for getting a hour out of a date command and validating hour to see if its > 7 and < 16. if hours is >7 and <16 then assign a variable value of 0730 and if hour is >16 then assign a variable value of 1630? Help appreciated. Thanks in advance.... (9 Replies)
Discussion started by: zulfikarmd
9 Replies

3. Shell Programming and Scripting

Compare file timestamp with current date. Diff must be 1 hour.

Hello, I've created the script below to compare the content of two files with a delay of an hour. After an hour, the lines that exist in both files, will be printed and executed. The script now uses a counter to countdown 50 minutes. But what I would prefer is to check the file timestamp of... (3 Replies)
Discussion started by: taipan
3 Replies

4. Shell Programming and Scripting

how to obtain date and day of the week from `date` command

Hi, does anybody know how to format `date` command correctly to return the day of the week? Thanks -A I work in ksh.... (1 Reply)
Discussion started by: aoussenko
1 Replies

5. Shell Programming and Scripting

Generate last hour date in YYYYmmddHH

can some one show me how to generate last hour of date for example: 2012010100 -> 2011123123 // check first date of a new year 2008030100 -> 2008022923 // check leap year as well And also no perl code is restricted. This is what i am facing issue now, can someone provides help to me. (4 Replies)
Discussion started by: alvin0618
4 Replies

6. Shell Programming and Scripting

finding the previous day date and creating a file with date

Hi guys, I had a scenario... 1. I had to get the previous days date in yyyymmdd format 2. i had to create a file with Date inthe format yyyymmdd.txt format both are different thanks guys in advance.. (4 Replies)
Discussion started by: apple2685
4 Replies

7. Red Hat

Cron entry for every 10 mints on business day business hour

Could you “crontab” it to run every 10 minutes on work days (Mo - Fr) between 08:00 and 18:00 i know to run every 10 mints but can any one guide me how to achieve the above one (2 Replies)
Discussion started by: venikathir
2 Replies

8. Shell Programming and Scripting

Finding/Grep on files with date and hour in the file name

Hi, I have a folder structure as follows, DATA -> 2012-01-01 -> 00 -> ABC_2012-01-03_00.txt -> 01 -> ABC_2012-01-03_01.txt -> 02 -> ABC_2012-01-03_02.txt ... -> 23 -> ABC_2012-01-03_02.txt -> 2012-01-02 -> 2012-01-03 So the dir DATA contains the above hierarchy, User input Start and... (6 Replies)
Discussion started by: mihirvora16
6 Replies

9. AIX

Need to get the next day's date of the user entered date

I need to get the next day's date of the user entered date for example: Enter date (yyyy/mm/yy): 2013/10/08I need to get the next day's date of the user entered date Desired Output: 2013/10/09Though there are ways to achieve this is Linux or Unix environment (date command) ,I need to... (1 Reply)
Discussion started by: rpm120
1 Replies

10. UNIX for Beginners Questions & Answers

Replace date in file every day with current date

I Have text like XXX_20190908.csv.gz need to replace Only date in this format with current date every day Thanks! (1 Reply)
Discussion started by: yamasani1991
1 Replies
DATE(1) 						    BSD General Commands Manual 						   DATE(1)

NAME
date -- display or set date and time SYNOPSIS
date [-ajnu] [-d date] [-r seconds] [+format] [[[[[[CC]yy]mm]dd]HH]MM[.SS]] DESCRIPTION
date displays the current date and time when invoked without arguments. Providing arguments will format the date and time in a user-defined way or set the date. Only the superuser may set the date. The options are as follows: -a Use adjtime(2) to change the local system time slowly, maintaining it as a monotonically increasing function. -a implies -n. -d date Parse the provided human-described date and time and display the result without actually changing the system clock. (See parsedate(3) for examples.) -j Parse the provided canonical representation of date and time (described below) and display the result without actually changing the system clock. -n The utility timed(8) is used to synchronize the clocks on groups of machines. By default, if timed is running, date will set the time on all of the machines in the local group. The -n option stops date from setting the time for other than the current machine. -r seconds Print out the date and time that is seconds from the Epoch. -u Display or set the date in UTC (universal) time. An operand with a leading plus (+) sign signals a user-defined format string which specifies the format in which to display the date and time. The format string may contain any of the conversion specifications described in the strftime(3) manual page, as well as any arbitrary text. A <newline> character is always output after the characters specified by the format string. The format string for the default display is: %a %b %e %H:%M:%S %Z %Y If an operand does not have a leading plus sign, it is interpreted as a value for setting the system's notion of the current date and time. The canonical representation for setting the date and time is: CC The first two digits of the year (the century). yy The second two digits of the year. If yy is specified, but CC is not, a value for yy between 69 and 99 results in a CC value of 19. Otherwise, a CC value of 20 is used. mm The month of the year, from 01 to 12. dd The day of the month, from 01 to 31. HH The hour of the day, from 00 to 23. MM The minute of the hour, from 00 to 59. SS The second of the minute, from 00 to 61. Everything but the minutes is optional. Time changes for Daylight Saving and Standard time and leap seconds and years are handled automatically. ENVIRONMENT
The following environment variables affect the execution of date: TZ The timezone to use when displaying dates. See environ(7) for more information. FILES
/etc/localtime Symlink pointing to system's default timezone information file in /usr/share/zoneinfo directory. /var/log/wtmp A record of date resets and time changes. /var/log/messages A record of the user setting the time. EXAMPLES
The command: date '+DATE: %m/%d/%y%nTIME: %H:%M:%S' will display: DATE: 11/21/87 TIME: 13:36:16 The command: date 8506131627 sets the date to ``June 13, 1985, 4:27 PM''. The command: date 1432 sets the time to 2:32 PM, without modifying the date. DIAGNOSTICS
Exit status is 0 on success, 1 if unable to set the date, and 2 if able to set the local date, but unable to set it globally. Occasionally, when timed(8) synchronizes the time on many hosts, the setting of a new time value may require more than a few seconds. On these occasions, date prints: 'Network time being set'. The message 'Communication error with timed' occurs when the communication between date and timed fails. SEE ALSO
adjtime(2), gettimeofday(2), settimeofday(2), parsedate(3), strftime(3), utmp(5), timed(8) R. Gusella and S. Zatti, TSP: The Time Synchronization Protocol for UNIX 4.3BSD. STANDARDS
The date utility is expected to be compatible with IEEE Std 1003.2 (``POSIX.2''). BSD
November 15, 2006 BSD
All times are GMT -4. The time now is 03:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy