将日期转换为指定格式和两个日期比较 [英] Converting date to specified format and two date comparisions

查看:47
本文介绍了将日期转换为指定格式和两个日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个日期:

$Date1

format: yyyy-mm-dd hh:mm:ss eg:  2013-05-21 07:47:21

$Date2

format: Day Month date hh:mm:ss yyyy eg: Thu Aug  1 09:53:40 2013

如何比较 $Date1 和 $Date2?

How to compare $Date1 and $Date2?

我想做以下操作:

if($Date2>=$Date1){//做一些操作}

if($Date2>=$Date1){ //Do some operation }

但我无法进行比较,因为两个日期的格式不同.

But I am not able to make comparison as the two dates are in different Format.

推荐答案

怎么样:

use Time::Piece;
my $d1 = '2013-05-21 07:47:21';
my $d2 = 'Thu Aug  1 09:53:40 2013';
my $t1 = Time::Piece->strptime($d1, "%Y-%m-%d %T");
my $t2 = Time::Piece->strptime($d2, "%c");
say $t2->epoch - $t1->epoch;

输出:

6228379

Time::Piece 文档

这篇关于将日期转换为指定格式和两个日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆