moment.js (JavaScript) vs Calendar (Java) vs DateTime (C #)

I'm writing it because I'm always confused because it straddles the program language. The reason why JavaScript is moment.js instead of the standard Date type is that I'm using moment now.

Left align moment.js(JavaScript) Calendar(Java) DateTime(C#)
Perspective from a string moment(text, format) Not in Calendar(Substitute with DateFormat) DateTime.Parse(text, culture)
Get the year year() get(Calendar.YEAR) Year
Get the month month() ※0〜 get(Calendar.MONTH) ※0〜 Month ※1〜
Get the day date() get(Calendar.DATE) Day
Get the day of the week day() SUN:0〜 get(Calendar.DAY_OF_WEEK) SUN:1〜 DayOfWeek SUN:0〜
Format to string format(format) Substitute with DateFormat ToString(format)

sample

January 1, 2018 is "Monday". Also, parse and format may depend on Locale, Culture, etc., so I won't mention them here, but be careful!

Moment.js(Node.js v8.9.0)

code

const moment = require('moment');

const m = moment('20180101', 'YYYYMMDD');
console.log(`Year=${m.year()}`);
console.log(`Month=${m.month()}`);
console.log(`Day=${m.date()}`);
console.log(`Day of the week=${m.day()}`);    // <---------Day of the weekが day()I know you can get it with ...
console.log(`Plastic surgery=${m.format('YYYY MM month DD day')}`);

result

Year=2018
Month=0                  <---------Monthは0から
Day=1
Day of the week=1                <---------Sunday=From 0
Plastic surgery=January 01, 2018

Calendar(Java v1.8.0)

code

//import java.text.DateFormat;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.Calendar;
//import java.util.Locale;

DateFormat df = new SimpleDateFormat("yyyy/MM/dd", Locale.JAPAN);
Date d = df.parse("2018/01/01");
Calendar cal = Calendar.getInstance();
cal.setTime(d);

System.out.println("Year=" + cal.get(Calendar.YEAR));
System.out.println("Month=" + cal.get(Calendar.MONTH));
System.out.println("Day=" + cal.get(Calendar.DATE));
System.out.println("Day of the week=" + cal.get(Calendar.DAY_OF_WEEK));
System.out.println("Plastic surgery=" + df.format(cal.getTime()));

result

Year=2018
Month=0                  <---------Monthは0から
Day=1
Day of the week=2                <---------Sunday=From 1
Plastic surgery=January 01, 2018

DateTime(C# / .NET Core 2.1)

code

var d = DateTime.Parse("2018/01/01");
Console.WriteLine($"Year={d.Year}");
Console.WriteLine($"Month={d.Month}");
Console.WriteLine($"Day={d.Day}");
Console.WriteLine($"Day of the week={d.DayOfWeek}({(int)d.DayOfWeek})");
Console.WriteLine($"Plastic surgery={d.ToString("yyyy year MM month dd day")}");  // <------Only MM is capitalized

result

Year=2018
Month=1             <---------Monthは1から
Day=1
Day of the week=Monday(1)   <---------Sunday=From 0(Enum is also possible)
Plastic surgery=January 01, 2018

reference

Moment.js

Calendar(Java)

DateTime(C#/.NET)

Recommended Posts

moment.js (JavaScript) vs Calendar (Java) vs DateTime (C #)
Java, JavaScript, C # (difference in assignment)
Java vs. JavaScript: What ’s the Difference?
Differences in writing Java, C # and Javascript classes
Java and JavaScript
Java --StringBuilder vs StringBuffer