DateTime
** "looks JavaScript, brain (contents) Ruby, (stability is AC / DC)" ** Scripting language Kinx ). The library is the life of the language. So how to use the library.
This time it's DateTime. I made it available in Range.
using DateTime
The DateTime library is not built-in, so use the using directive to load it explicitly.
using DateTime;
Instantiation is basically done by creating a new DateTime object.
new DateTime ()
... instantiated at the current timenew DateTime (dateString)
... Parsing and instantiating a stringnew DateTime (Unixtime)
... Instantiated from UNIX epoch timenew DateTime (year, month, day [, hour, minute, second])
... Instantiate by specifying date and time information individuallyHowever, you can also do the following (just return it as new internally). Please use the one you like.
DateTime.parse(...)
DateTime(...)
In addition, dateString
interprets the following format.
"2020-01-01"
、"2020-1-1"
"2020/01/01"
、"2020/1/1"
"2020-01-01T10:00:05"
、"2020-1-01T10:0:5"
"2020/01/01 10:00:05"
、"2020/1/01 10:0:5"
The DateTime object has the following methods.
Method | Operation overview |
---|---|
isLeapYear() |
Returns true for leap years |
unixtime() |
Returns the Unix epoch time of the current date and time |
datetime() |
Returns an object that represents the current date and time |
year() |
"Year" of the current date and time |
month() |
"Month" of the current date and time |
day() |
"Day" of the current date and time |
hour() |
"Time" of the current date and time |
minute() |
"Minute" of the current date and time |
second() |
"Second" of the current date and time |
weekday() |
"Week" of the current date and time (0:Sunday, 1:Monday, ..., 6:Saturday) |
isSunday() |
Returns true on Sunday |
isMonday() |
Returns true on Monday |
isTuesday() |
Returns true on Tuesday |
isWednesday() |
Returns true on Wednesday |
isThursday() |
Returns true on Thursday |
isFriday() |
Returns true on Friday |
isSaturday() |
Returns true on Saturday |
clone() |
Returns a copy of the datetime object |
addDay(day) |
Date and time objectday Advance the day (destructive) |
subDay(day) |
Date and time objectday Day back (destructive) |
addMonth(month) |
Date and time objectmonth Advance months (destructive) |
subMonth(month) |
Date and time objectmonth Month back (destructive) |
next() |
Returns a new date and time object representing the next day |
+(day) |
day Returns a new date and time object that represents the day after |
-(day) |
day Returns a new date and time object that represents the day before |
>>(month) |
month Returns a new date and time object that represents the month after |
<<(month) |
month Returns a new date and time object that represents the month ago |
<=>(dt) |
0:Same date and time,-1: dt Is the later date and time, 1: dt Is the earlier date and time |
format(fmtString) |
fmtString Format according to the format of. The supported formats are as follows.%YYYY% :4-digit year,%YY% :Double digit year%MM% :Double digit month,%M% :Month%DD% :Double digit day,%D% :Day%hh% :When it is 2 digits%h% :Time%mm% :2 digit minutes,%m% :Minutes%ss% :2 digit seconds,%s% :Seconds |
If you move a month with <<
or >>
and the same day does not exist in the corresponding month, the last day of that month is used instead.
using DateTime;
System.println(DateTime("2001-3-28") << 1); // 2001/02/28 00:00:00
System.println(DateTime("2001-3-31") << 1); // 2001/02/28 00:00:00
This may behave unexpectedly (along with Ruby), as follows:
using DateTime;
System.println(DateTime("2001-1-31") >> 2); // 2001/03/31 00:00:00
System.println(DateTime("2001-1-31") >> 1 >> 1); // 2001/03/28 00:00:00
System.println(DateTime("2001-1-31") >> 1 >> -1); // 2001/01/28 00:00:00
Range
To use it in Range, you should define the next
method and the<=>
method. So the DateTime object can be used in Range.
using DateTime;
(DateTime(2020,1,1)..DateTime(2020,1,10))
.each(&(d) => System.println(d));
Since it is ..
, the last day is included. In the case of ...
, the last day is not included.
2020/01/01 00:00:00
2020/01/02 00:00:00
2020/01/03 00:00:00
2020/01/04 00:00:00
2020/01/05 00:00:00
2020/01/06 00:00:00
2020/01/07 00:00:00
2020/01/08 00:00:00
2020/01/09 00:00:00
2020/01/10 00:00:00
Since it can be used in Range, it can be used as it is in for-in.
using DateTime;
for (var d in DateTime(2020,1,1)...DateTime(2020,1,10)) {
System.println(d);
}
A loop that does not include the last day.
2020/01/01 00:00:00
2020/01/02 00:00:00
2020/01/03 00:00:00
2020/01/04 00:00:00
2020/01/05 00:00:00
2020/01/06 00:00:00
2020/01/07 00:00:00
2020/01/08 00:00:00
2020/01/09 00:00:00
It's been about half a year since I started making it. You can do various things. Isn't your next goal to be able to enrich the library and create some kind of app? You may be able to find a place somewhere if you can quickly create apps for niche applications.
See you next time.