DATETime formatini o'tkazish mm/dd/yyyy dan yyyy/mm/dd oralig'ida

Kimdir menga bu datetime formatini yyyyMMdd formatida qanday qilib o'zgartirishi mumkinligini menga xabar bering

2/28/2017 12:02:04 AM

At the output I should get 20170228

Bu haqda biron bir maslahat bormi?

1
Qilishingiz kerak bo'lgan narsalarga nisbatan ko'proq ma'lumot berishingiz kerak. Siz allaqachon sinab ko'rgan narsalarni misollarni kiritishga harakat qiling. Tarixni mag'lubiyatga ajratish va keyin uni chiqazishingiz kerakmi yoki bu allaqachon biror DateTime obyekti.
qo'shib qo'ydi muallif Adam Carr, manba

6 javoblar

Agar siz allaqachon DataTime-ni ob'ekt sifatida tanlasangiz

string formattedDate = date.ToString("yyyyMMdd");

Agar siz oldin qiymatni ajratsangiz kerak.

string dateValue = "2/28/2017 12:02:04 AM";
string format = "M/d/yyyy hh:mm:ss tt";

DateTime dateTime = DateTime.ParseExact(dateValue, format,
            System.Globalization.CultureInfo.InvariantCulture);

For reference you can find a breakdown of the Custom Date and Time Format Strings

3
qo'shib qo'ydi
Maxsus Sana va vaqt formati satrlari haqida qo'shimcha ma'lumot qo'shildi
qo'shib qo'ydi muallif Adam Carr, manba

Agar siz allaqachon DataTime-ni ob'ekt sifatida tanlasangiz

string formattedDate = date.ToString("yyyyMMdd");

Agar siz oldin qiymatni ajratsangiz kerak.

string dateValue = "2/28/2017 12:02:04 AM";
string format = "M/d/yyyy hh:mm:ss tt";

DateTime dateTime = DateTime.ParseExact(dateValue, format,
            System.Globalization.CultureInfo.InvariantCulture);

For reference you can find a breakdown of the Custom Date and Time Format Strings

3
qo'shib qo'ydi
Maxsus Sana va vaqt formati satrlari haqida qo'shimcha ma'lumot qo'shildi
qo'shib qo'ydi muallif Adam Carr, manba

Sana formatini ko'rsatishingiz kerak.

Agar siz hozirgi vaqt uchun shunday qilishni xohlasangiz:

string dtime = DATETime.Now.ToString ("yyyy/mm/dd");

1
qo'shib qo'ydi

Agar siz C# 6 yoki undan keyingi versiyani (VS2015) dan foydalansangiz, maxsus formatli mag'lubiyat yordamida straginatsiyani foydalanib, DateTime moslamalarni osongina formatlashingiz mumkin. Siz qidirayotgan maxsus formatdagi magistral "yyyyMMdd" .

// create your preferred date and time in a new DateTime struct
DateTime yourDateTime = new DateTime(2017, 2, 28, 0, 2, 4);

// format yourDateTime as a string
string yourFormattedDateTime = $"{yourDateTime:yyyyMMdd}";

You can read more about interpolated strings at https://msdn.microsoft.com/en-us/library/dn961160.aspx, and, as previously mentioned by @Adam Carr, you can find more information on custom date and time format strings at https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

0
qo'shib qo'ydi

Bu siz uchun yaratgan yechim:

string format = "M/d/yyyy hh:mm:ss tt";
string dateString = "2/28/2017 12:02:04 AM";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime date = DateTime.ParseExact(dateString, format, provider);
string output = date.ToString("yyyyMMdd");
0
qo'shib qo'ydi

Bu siz uchun yaratgan yechim:

string format = "M/d/yyyy hh:mm:ss tt";
string dateString = "2/28/2017 12:02:04 AM";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime date = DateTime.ParseExact(dateString, format, provider);
string output = date.ToString("yyyyMMdd");
0
qo'shib qo'ydi