Solved

UNIX ksh parse year from date

Posted on 2010-11-09
6
1,069 Views
Last Modified: 2013-12-26
How to parse year fron date
0
Comment
Question by:fcarleton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:mikepflu
ID: 34098816
date +%Y
0
 

Author Comment

by:fcarleton
ID: 34098861
I need to expound more on the question.  How can I get year in YYYY format from date. A table of date formatting codes would be real helpful.  Patterned after Mikes reply: date +%Y.  That yeilds the YY segment of the full CCYY year value.
echo `date +%Y`
prints 10

Fc
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 20 total points
ID: 34098897
date +%y
prints 10
date +%Y
prints 2010
0
Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

 
LVL 12

Expert Comment

by:tel2
ID: 34099465
By the way, there's no need to:
    echo `date +%Y`
because
    date +%Y
automatically prints to stdout.
0
 
LVL 10

Expert Comment

by:TRW-Consulting
ID: 34099507
How about this:

  DateToParse="January 12, 1967"      # put whatever date you want here, in almost any format
  YEAR=`date -d "$DateToParse" +'%Y'`
  echo "The year is $YEAR"

0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 30 total points
ID: 34099981
The table of formatting codes is in "man date" or here (see attachment).
wmp

.
       %a      
            Displays the locale's abbreviated weekday name.
       %A
            Displays the locale's full weekday name.
       %b
            Displays the locale's abbreviated month name.
       %B
            Displays the locale's full month name.
       %c
            Displays the locale's appropriate date and time representation. This is the
            default.
       %C
            Displays the first two digits of the four-digit year as a decimal number (00-99).
            A year is divided by 100 and truncated to an integer.
       %d
            Displays the day of the month as a decimal number (01-31). In a two-digit field, a
            0 is used as leading space fill.
       %D
            Displays the date in the format equivalent to %m/%d/%y.
       %e
            Displays the day of the month as a decimal number (1-31). In a two-digit field, a
            blank space is used as leading space fill.
       %h
            Displays the locale's abbreviated month name (a synonym for %b).
       %H
            Displays the hour (24-hour clock) as a decimal number (00-23).
       %I
            Displays the hour (12-hour clock) as a decimal number (01-12).
       %j
            Displays the day of year as a decimal number (001-366).
       %k
            Displays the 24-hour-clock hour clock as a right-justified, space-filled number (
            0 to 23).
       %m
            Displays the month of year as a decimal number (01-12).
       %M
            Displays the minutes as a decimal number (00-59).
       %n
            Inserts a <new-line> character.

       %p
            Displays the locale's equivalent of either AM or PM.
       %r
            Displays 12-hour clock time (01-12) using the AM-PM notation; in the POSIX locale,
            this is equivalent to %I:%M:%S %p.
       %S
            Displays the seconds as a decimal number (00- 59).
       %s
            Displays the number of seconds since January 1, 1970, Coordinated Universal Time
            (CUT).
       %t
            Inserts a <tab> character.
       %T
            Displays the 24-hour clock (00-23) in the format equivalent to HH:MM:SS .
       %u
            Displays the weekday as a decimal number from 1-7 (Sunday = 7). Refer to the %w
            field descriptor.
       %U
            Displays week of the year(Sunday as the first day of the week) as a decimal
            number[00 - 53] . All days in a new year preceding the first Sunday are considered
            to be in week 0.
       %V
            Displays the week of the year as a decimal number from 01-53 (Monday is used as
            the first day of the week). If the week containing January 1 has four or more days
            in the new year, then it is considered week 01; otherwise, it is week 53 of the
            previous year.
       %w
            Displays the weekday as a decimal number from 0-6 (Sunday = 0). Refer to the %u
            field descriptor.
       %W
            Displays the week number of the year as a decimal number (00-53) counting Monday
            as the first day of the week.
       %x
            Displays the locale's appropriate date representation.
       %X
            Displays the locale's appropriate time representation.
       %y
            Displays the last two numbers of the year (00-99).
       %Y
            Displays the four-digit year as a decimal number.
       %Z
            Displays the time-zone name, or no characters if no time zone is determinable.
       %%
            Displays a % (percent sign) character.
       %Ec
            Displays the locale's alternative appropriate date and time representation.
       %EC
            Displays the name of the base year (or other time period) in the locale's
            alternative representation.
       %Ex

            Displays the locale's alternative date representation.
       %EX
            Displays the locale's alternative time representation.
       %Ey
            Displays the offset from the %EC field descriptor (year only) in the locale's
            alternative representation.
       %EY
            Displays the full alternative year representation.
       %Od
            Displays the day of the month using the locale's alternative numeric symbols.
       %Oe
            Displays the day of the month using the locale's alternative numeric symbols.
       %OH
            Displays the hour (24-hour clock) using the locale's alternative numeric symbols.
       %OI
            Displays the hour (12-hour clock) using the locale's alternative numeric symbols.
       %Om
            Displays the month using the locale's alternative numeric symbols.
       %OM
            Displays minutes using the locale's alternative numeric symbols.
       %OS
            Displays seconds using the locale's alternative numeric symbols.
       %Ou
            Displays the weekday as a number in the locale's alternative representation
            (Monday=1).
       %OU
            Displays the week number of the year using the locale's alternative numeric
            symbols. Sunday is considered the first day of the week.
       %OV
            Displays the week number of the year using the locale's alternative numeric
            symbols. Monday is considered the first day of the week.
       %Ow
            Displays the weekday as a number in the locale's alternative representation
            (Sunday =0).
       %OW
            Displays the week number of the year using the locale's alternative numeric
            symbols. Monday is considered the first day of the week.
       %Oy
            Displays the year (offset from %C) in alternative representation.

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question