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.
In reply to Re: date and time using localtime
by thanos1983
in thread date and time using localtime
by sreek3502
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |