I want to display the system date in Japanese calendar and Japan time.

Asked 2 years ago, Updated 2 years ago, 49 views

I'd like to get the system date in C# and display "Heisei y mm month dd day hhhh mm minutes ss seconds".
I implemented it as follows, but the results are

6/30/2016 10:03:06 AM
Heisei June 30, 28 at 10:03:06

Yes (run on June 30, 2016 at 19:03)
The year will be converted into the Japanese calendar without any problems (2016 → Heisei 28).
However, I would like to display the time in jst instead of utc (I would like to display 19:03:06 seconds).
I've tried various ways to get Timezone, but it doesn't work very well.
How do I implement it?

By the way, regarding the running environment, the operating system is XP and the running environment is paiza.io(https://paiza.io/projects/new).This may also be the reason why it doesn't work...I am a java engineer and have a java development environment, but there is no C# environment.
I'm asking because I need some C#s to deploy test automation tools.

public class Hello {
public static void Main() {

     // Get current time in local time

    System.Globalization.CultureInformation=new System.Globalization.CultureInfo("ja-JP", true);
    culture.DateTimeFormat.Calendar=new System.Globalization.JapaneseCalendar();



    System.DateTime now=System.DateTime.Now;

    System.Console.WriteLine(now);
    // OUTPUT METHOD OF Japanese CALENDAR
    System.Console.WriteLine(now.ToString("ggyy MM month dd day HH hours mm minutes sssec", culture));
}

}

c#

2022-09-29 21:59

2 Answers

CultureInfo does not contain time zone information, so you must use the TimeZoneInfo class separately.This conversion cannot be done at ToString() time, so you will have to convert it in advance.

DateTime now = DateTime.Now;
// Get Asia/Tokyo Time Zone Information
TimeZoneInfojst=TimeZoneInfo.FindSystemTimeZoneById("Asia/Tokyo");
// Convert from a time zone pointed to by the Kind property of the source DateTime to a specified time zone
DateTime now_jst = TimeZoneInfo.ConvertTime(now_jst,jst);
Console.WriteLine(now_jst.ToString());

The code above specifies Asia/Tokyo as the ID for JST, which is for non-Windows environments using Mono.For Windows environments, use Tokyo Standard Time.

However, in a Windows environment set to Japanese, DateTime.Now also returns the time in JST, and the Japanese calendar also displays Heisei instead of Heisei.This behavior differs between Windows+ .NET Framework and non-Windows+Mono such as paiza.io, so if you need to eventually run it on Windows, we recommend that you create a local development environment.

See also


2022-09-29 21:59

The computer time zone does not appear to be set as desired.Unarist answered, but in the case of Windows, the time zone list is obtained from the registry, and in the case of UNIX, tzinfo is used, so the method of specification is different.
If you feel uncomfortable, assume that Japan Standard Time is +9 hours

// Move 9 hours to UTC time
varnow=DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(+9));

You can write

Also, there are two or three types of display methods

How to specify individually when stringing

var cultureInfo=new CultureInfo("ja-JP");
cultureInfo.DateTimeFormat.Calendar=new JapaneseCalendar();
Console.WriteLine(now.ToString("ggyy MM month dd day HH hours mm minutes sssec", cultureInfo));

How to Format Threads

CultureInfo.CurrentCulture=new CultureInfo("ja-JP");
CultureInfo.CurrentCulture.DateTimeFormat.Calendar=new JapaneseCalendar();
Console.WriteLine("{0:ggyyy MM month dd day HH hours mm minutes sssec}", now);
Console.WriteLine ($"{now:ggyyy MM month dd day HH hours mm minutes ss}");

The last line is the interpolation string.


2022-09-29 21:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.