Hello sreek3502,

Welcome to the Monastery. Fellow monks have answered your question, but I wanted to add my favorite formatting date module Date::Manip. I discovered the module fairly recent but I was blast by the capabilities. It may look a bit complicated at the beginning but more you experiment easier it becomes.

Having said that, I think you are looking for something like that?

#!/usr/bin/perl use strict; use warnings; use Date::Manip; use feature 'say'; my $dateLocal = ParseDate('now'); # say $dateLocal; my $str = UnixDate($dateLocal,"It is now %T on %b %e, %Y."); say $str; my $str_second = UnixDate($dateLocal,"It is now %T on %B %e, %Y."); say $str_second; my $str_decimal = UnixDate($dateLocal,"It is now decimal %T on %B %d, +%Y."); say $str_decimal; my $deltastr = "12 hours ago"; my $date_past = DateCalc($dateLocal,$deltastr); # say $date; my $str_past = UnixDate($date_past,"It was 12 hours ago %T on %B %d, % +Y."); say $str_past; __END__ $ perl test.pl It is now 11:05:12 on Oct 9, 2017. It is now 11:05:12 on October 9, 2017. It is now decimal 11:05:12 on October 09, 2017. It was 12 hours ago 23:05:12 on October 08, 2017.

You can modify the output of the date by using any of the POSIX::strftime::GNU/FORMAT parameters.

Or an alternative to simplify it even more is to use the POSIX::strftime::GNU. Check out also the POSIX::strftime::GNU/FORMAT on how to play around with the stdout format based on your desire.

Sample of code:

#!/usr/bin/perl use strict; use warnings; use feature 'say'; use POSIX 'strftime'; use POSIX::strftime::GNU; say POSIX::strftime('%a, %d %b %Y %T %z', localtime); __END__ $ perl test.pl Mon, 09 Oct 2017 10:57:25 +0200

Depending on what you want to do, and what is easier for you to create / maintain you can choose to apply.

I hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: date and time using localtime by thanos1983
in thread date and time using localtime by sreek3502

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.