2

I'm having issues trying to get the full Japanese date to display correctly in my code (I need the code to work for all regional languages around the world). In the highlighted part below is how I want it to display but instead I get: 2015?5?6? 11:05:21

This date is later written to an MS SQL server in which it also displays the question marks despite having the correct collation required

Japanese Full Date Format

currDate = "Date: " + DateTime.Now.ToString("F");

Console.WriteLine("Current Date: " + currDate);

Any help is appreciated. Thanks

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user3424480
  • 362
  • 2
  • 6
  • 19

5 Answers5

2

Your console is not able to display the char you want to display because by default it's set to ASCII char. This should display your Japanese need :

Console.OutputEncoding = System.Text.Encoding.Unicode

pixelbadger is right: Your font may not be able to display such characters even with the good encoding. Please, see this answer which is about how to display japanese Kanji in cmd windows?

Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • The console doesn't support kanji glyphs, even if you change the output encoding. There is a workaround [here](http://stackoverflow.com/questions/3780378/how-to-display-japanese-kanji-inside-a-cmd-window-under-windows). – pixelbadger May 06 '15 at 10:25
  • @Joze you're totally right. That's a shame I confound both and I present my apologies to the whole Asian community :) – Thomas Ayoub May 06 '15 at 10:26
2

Very late to the party, but none of the answers are showing you how to actually display in Japanese date format, just western format with some kanji thrown in for good measure, ie 2018年9月24日

So I hope this helps someone in the future. The correct way to show Japanese date is as follows.

public void GetDate()
{
    CultureInfo culture = new CultureInfo("ja-JP", true);
    culture.DateTimeFormat.Calendar = new JapaneseCalendar();

    DateTime newDate = new DateTime(2018, 9, 24);
    string currDate = newDate.ToString("ggyy年M月d日", culture);
    Console.WriteLine("Current Date: " + currDate);

   // OutPut:"Current Date: 平成30年9月24日"
}

So instead of this 2018年9月24日 you get this 平成30年9月24日 which is the correct format for Japanese calendar dates. I hope this helps.

KyloRen
  • 2,691
  • 5
  • 29
  • 59
  • After 1/5/2019 Japan has a new king, so it must be `令和元年05月01日`. But when I use my code, the result is `平成31年05月01日` – Learning May 09 '19 at 07:45
  • @Learning, just wait for the Microsoft update or make a converter yourself. – KyloRen May 09 '19 at 14:13
1

you can use the CultureInfo class so you specify culture on a per string basis:

CultureInfo culture = new CultureInfo("en-US");
string.Format(culture, "SDate in US format: {0:d}", DateTime.Now);
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Jack
  • 350
  • 2
  • 15
1

You need to specify the culture. For Japanese, it is ja-JP

Try rewriting your code as

CultureInfo ci = new CultureInfo("ja-JP");
currDate = "Date: " + DateTime.Now.ToString("F",ci);
Console.WriteLine("Current Date: " + currDate);
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Adersh M
  • 596
  • 3
  • 19
  • I'm not sure it's about culture, since if it was displaying in the default (which is en-US if I'm correct) then the `?` would not have been there – Thomas Ayoub May 06 '15 at 10:28
  • The culture depends on what is set on ur system. If it's en-US, then it will be the default. Also, the format depends on what you set for short date and long date for in your system. – Adersh M May 06 '15 at 10:32
  • Well no. Then someone told me I was wrong and now I'm testing, if it doesn't work I'll delete my answer event if it has a positive vote count :) – Thomas Ayoub May 06 '15 at 10:49
0

You dont have the Japanese Table installed on your windows.

Install the Japanese converion table from Windows Update, select it in the Regional Settings tab in Control Panel and reboot. After that it should work correctly.

A good way to test if the table installed correctly write 'chcp 932' in CMD, it should show Japanese characters. Another thing to keep in mind is that the font your console is using supports the Japanese characters, the standard UTF-8 is able to render Japanese.

Freijlord
  • 96
  • 7