Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Linux Commands»date command in Linux with Examples

    date command in Linux with Examples

    RahulBy RahulDecember 8, 20203 Mins Read

    date is an useful Linux command to view or change current date on system. All the Linux operating systems are synced with the NTP (Network Time Protocol) servers. So its rarely required to configure system time manually. You only need to change timezone as per your requirements.

    This tutorial will described you to uses of date command on Linux system.

    Syntax:

    Usage: date [OPTION]... [+FORMAT]
      or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
    

    View Current Date

    The default date command displays the current date and time on standard output. This date/time will be in default system time zone.

    Open a terminal (CTRL + ALT + T) and type:

    date
    
    Output> Tue Dec  8 15:08:21 IST 2020
    

    The above command shows current time in EST timezone, which is the default timezone of my Linux system.

    date Command Examples

    1. Show date time in UTC/GMT:

      Use -u or --utc or --universal option with date command to display current time in UTC(Coordinated Universal Time ) or GMT(Greenwich Mean Time) time zone.

      date -u 
      
      Output> Tue Dec  8 09:38:34 UTC 2020
      
    2. View past dates on specific days:

      You can use --date command line option to view date in past. Like what was the date 7 days ago, 20 days ago, 2 month ago, last Friday, yesterday and so on.

      date --date="yesterday"
      
      Output> Mon Dec  7 15:08:52 IST 2020
      
      date --date="7 days ago"		# Tue Dec  1 15:09:23 IST 2020
      date --date="20 days ago"		# Wed Nov 18 15:09:42 IST 2020
      date --date="5 months ago"		# Wed Jul  8 15:09:55 IST 2020
      date --date="last friday"		# Fri Dec  4 00:00:00 IST 2020
      
    3. View future dates:
      Similarly, you can view dates in future. For example, what will be date on tomorrow, next friday, after 20 days, after 3 months and so on.

      date --date="tomorrow"			# Wed Dec  9 15:10:50 IST 2020
      date --date="next friday"		# Fri Dec 11 00:00:00 IST 2020
      date --date="+20 days"			# Mon Dec 28 15:20:08 IST 2020
      date --date="+3 months"		# Mon Mar  8 15:20:18 IST 2021
      
      

      You can use this feature to detect month end. For example, If tomorrow is 01, then today is the last date of monthly. Which can be useful for running cronjobs on end of the months.

      [ `date --date="tomorrow" +"%d"` -eq 09 ] &&  echo "Today is last day of month"
      
    4. View date in other timezone:
      Use TZ environment variable to view date in other timzone without changing system timezone.

      TZ="EST" date				# Tue Dec  8 04:50:28 EST 2020
      TZ='America/Los_Angeles' date		# Tue Dec  8 01:50:55 PST 2020
      
    5. Print date in specific format:
      The default date command print complete date and time including day and timezone. You can also customize it to print specific date format.

      date +"%d-%m-%y"			# 08-12-20
      date +"%y-%m-%d"			# 20-12-08
      date +"%b %d, %Y"			# Dec 08, 2020
      date +"%Y-%m-%d %H:%M:%S"		# 2020-12-08 08:36:40
      
    6. View file modification time:
      Use -r option with Linux date command to view last modification time of a file. Y

      date -r /etc/hosts
      
      Output> Sat Feb 15 11:44:01 IST 2020
      
    7. Conclusion

      In this tutorial, you have learned about date command in Linux with useful examples. Hope you understand the uses of date command in Linux system.

      If you like this post, please od share this with your social network.

    command date
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Check PHP Version (Apache/Nginx/CLI)
    Next Article How to Install Spotify on Ubuntu 20.04

    Related Posts

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Tee Command in Linux with Examples

    4 Mins Read

    How to Scan Open Ports with Nmap

    5 Mins Read

    Handling filenames with spaces in Linux

    3 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.