- Perl 6 Deep Dive
- Andrew Shitov
- 179字
- 2021-07-03 00:05:55
Using the DateTime data type
The usage of the DateTime data type is very similar to working with the Date type. In the DateTime objects, new fields to deal with time appear. We can see that in the following code snippet:
my $dt = DateTime.new(
year => 2017,
month => 7,
day => 19,
hour => 1,
minute => 46,
second => 48);
say $dt; # 2017-07-19T01:46:48Z
say $dt.year; # 2017
say $dt.month; # 7
say $dt.day; # 19
say $dt.hour; # 1
say $dt.minute; # 46
say $dt.second; # 48
To create a new DateTime object and set it to the current moment, use the now constructor, as shown here:
my $dt = DateTime.now;
say $dt; # 2017-07-19T01:44:00.301537+02:00
The hh-mm-ss and yyyy-mm-dd methods generate formatted strings for time and date:
say $dt.yyyy-mm-dd; # 2017-07-19
say $dt.hh-mm-ss; # 01:45:44
Be careful with printing seconds. The second method returns a floating-point number containing fractions of a second. To get an integer value, use the whole-second method:
my $now = DateTime.now;
say $now.second; # 43.3285570144653
say $now.whole-second; # 43
推薦閱讀
- Getting Started with Citrix XenApp? 7.6
- iOS面試一戰(zhàn)到底
- HTML5移動Web開發(fā)技術
- 深入淺出Spring Boot 2.x
- NumPy Essentials
- PHP+MySQL+Dreamweaver動態(tài)網(wǎng)站開發(fā)實例教程
- Elasticsearch for Hadoop
- R Deep Learning Cookbook
- 全棧自動化測試實戰(zhàn):基于TestNG、HttpClient、Selenium和Appium
- SQL Server數(shù)據(jù)庫管理與開發(fā)兵書
- 持續(xù)輕量級Java EE開發(fā):編寫可測試的代碼
- Test-Driven Development with Django
- Spring Boot實戰(zhàn)
- Python網(wǎng)絡爬蟲技術與應用
- INSTANT Apache ServiceMix How-to