APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2003/12/03 date


© December 2003 Tony Lawrence

Displays or sets the date.

Setting the date is often difficult for new users: the syntax can be strange and the man page can be confusing. Worse, it is different on the various Unix variants. To set August 25th 2003, 9:15 PM, requires:


I often install a script to help:

#!/bin/sh
# Setdate for Linux
echo -n "Enter Date or leave blank for `date +%m/%d/%y`: " 
read mydat
echo -n "Enter Time or leave blank for `date +%H:%M`: " 
read mytime
if [ "$mydat" != "" ]
then
mmdd=`echo $mydat | sed 's/\///g;s/..$//'`
yr=`echo $mydat | sed 's/\///g;s/^....//'`
else
mmdd=`date +%m%d`
yr=`date +%y`
fi

if [ "$mytime" != "" ]
then
newtime=`echo $mytime | sed 's/://g'`
else
newtime=`date +%H%M`
fi
date "$mmdd$newtime$yr"
 

As the above script shows, "date" can be very precise in its output.

On systems where you have it, GNU date ("date -d yesterday") or "datemath" is the easiest way to do such things as finding yesterday's or tomorrow's date. Without that, using Perl or other higher level languages is easier than forcing the issue with "date", but it can be done: https://aplawrence.com/Unix/yesterday.html.

With Perl and other languages, dates are held as they actually are on the system itself: as seconds since January, 1970. This makes date math easy: to get to tommorow, add one day's worth of seconds (60 * 60 * 24), and so on. For example:

#!/usr/bin/perl
$t=time;
print "Today is " . localtime($t);
print "\nYesterday was " . localtime($t - (3600 * 24));
print "\nTomorrow will bes " . localtime($t + (3600 * 24));
 

Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> date


Inexpensive and informative Apple related e-books:

iOS 8: A Take Control Crash Course

El Capitan: A Take Control Crash Course

Take Control of iCloud, Fifth Edition

Take Control of High Sierra

Take Control of Numbers




More Articles by © Tony Lawrence




Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us


Printer Friendly Version





He who hasn't hacked assembly language as a youth has no heart. He who does as an adult has no brain. (John Moore)




Linux posts

Troubleshooting posts


This post tagged:

Scripting

Shell

UnixWords



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode





SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)
www.SCOsales.com