Convert a string (format: yyyyymmddhhmmss) to datetime [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to convert string with unusual format into datetime
How can I convert a string to DateTime in c#? example:
string s = "20070406000000";
How can I convert that string into a DateTime?

Use the ParseExact or TryParseExact method:
DateTime t = DateTime.ParseExact("20070406000000", "yyyyMMddHHmmss", CultureInfo.InvariantCulture);

This CodeProject article explains how to do this.
String MyString = "1999-09-01 21:34 PM";
DateTime MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", CultureInfo.InvariantCulture);

Use DateTime.ParseExact()
CultureInfo provider = CultureInfo.InvariantCulture;
var date = DateTime.ParseExact(theDateString, "yyyyMMddHHmmss", provider);

You will need to use the DateTime.ParseExact function to tell it what the format of the string is so it knows how to convert it.
string strDate = "20070406000000";
string strDateTimeFormat = "yyyyMMddHHmmss";
DateTime objDate = DateTime.ParseExact(strDate, strDateTimeFormat, DateTimeFormatInfo.InvariantInfo);

Related

how to change date format to mm/dd/yyyy to dd/mm/yyyy in c# [duplicate]

This question already has answers here:
How to change date format from DD/MM/YYYY or MM/DD/YYYY to YYYY-MM-DD?
(4 answers)
Closed 5 years ago.
im gettin the date in this format "3/16/2017 10:53:44 PM", how to get date in this format "16/3/2017 10:53:44 PM"
sample data
"timestamp":"2017-03-16T17:23:44.860Z"
desired op
"16/3/2017 10:53:44 PM"
code
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime(bin.timestamp), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
data type
public String UpdatedTime { get; set; }
Or Try to parse the datetime.
For example
DateTime dateformat = DateTime.ParseExact(Convert.ToDateTime(bin.timestamp), "dd/M/yyyy h:mm:ss tt",CultureInfo.InvariantCulture);
binModel.UpdatedTime = TimeZoneInfo.ConvertTimeFromUtc(dateformat,TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")).ToString();
You need to convert it to string:
binModel.UpdatedTime = bin.timestamp.ToString("dd/MM/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture));
For more information: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
A DateTime has no display format.
Only string represnetations of DateTime does.
You can specify the desired format as a parameter in one of the the ToString method overloads of the DateTime struct:
var s = DateTime.Now.ToString("dd/M/yyyy hh:mm:ss tt");
Now s contains the folowing string: "23/3/2017 09:01:32 AM" (my local time)
var currentDate = DateTime.Now.ToString("dd/MM/yyyy");

DateTime format conversion mm/dd/yyyy to yyyy/mm/dd

Can someone please let me know how do I convert this datetime format into yyyyMMdd
2/28/2017 12:02:04 AM
At the output I should get 20170228
Any advice on this?
If you already have the DateTime as an object
string formattedDate = date.ToString("yyyyMMdd");
If you need to parse the value first.
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
You need to specify the format of the date.
If you want it for the current time you can try like this :
string dtime = DateTime.Now.ToString("yyyy/MM/dd");
This is the solution I have come up with for you:
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");
If you're using C# 6 or later (VS2015), you can format DateTime objects easily by using string interpolation using a custom format string. The custom format string that you're looking for is "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

How to convert date format in c#? [duplicate]

This question already has answers here:
convert datetime to date format dd/mm/yyyy
(15 answers)
Closed 6 years ago.
I am getting data from XML in format of MM/dd/yyyy HH:mm:ss tt (e.g 7/21/2016 5:43:03 PM) but I want to convert it into date format of only dd/MM/yyyy (e.g 21/7/2016)
You could try something like this:
var dt = DateTime.ParseExact(input, "M/dd/yyyy HH:mm:ss tt", CultureInfo.InvariantCulture)
.ToString("dd/MM/yyyy");
Initially, using the ParseExact you create a DateTime object based on your input and later using the ToString you create the string representation of your DateTime in the specified format.
For info about the DateTime.ParseExact have a look here.
You can do this as a quick trick:
string date = "7/21/2016 5:43:03 PM";
var dateOnly = DateTime.Parse(date).ToString("MM/dd/yyyy");
Explanation:
DateTime.Parse(date) <--- Converts the string to DateTime object.
ToString("MM/dd/yyyy") <--- Converts the DateTime Object to the specified format.
string date = "7/21/2016 5:43:03 PM";//Date from XML
DateTime dateTime;
if (DateTime.TryParse(date, out dateTime))
{
var dateOnly = dateTime.ToString("dddd, MMMM dd, yyyy");
}

C# string to SqlDateTime: how to set format for recognition?

I need to use
SqlDateTime.Parse(val)
where val is a string such as " 23.3.1992 00:00:00 ".
The string is in European format, that is, day precedes month. However Parse wants "American" format. How I can tell it to use particular datetime format / locale?
Thanks in advance!
Try this:
string val = "23.12.1992 00:00:00";
// Parse exactly from your input string to the native date format.
DateTime dt = DateTime.ParseExact(val, "dd.M.yyyy hh:mm:ss", null);
// Part to SqlDateTime then
System.Data.SqlTypes.SqlDateTime dtSql = System.Data.SqlTypes.SqlDateTime.Parse(dt.ToString("yyyy/MM/dd"));
This could be done in one statement, but just separated for illustration.
Have you tried DateTime instead of SQLDateTime
DateTime d = DateTime.Parse(val);
String s = d.ToString(CultureInfo.CreateSpecificCulture("en-US"));
Can you try this ?
string valInEuropean = "23.3.1992 00:00:00";
DateTime dateInEuropean = DateTime.Parse(valInEuropean);
string valInAmerican = dateInEuropean.ToString("yyyy-MM-dd HH:mm:ww");
For converting a string to datetime object when the format is known(in this case )
use
DateTime dwweek = DateTime.ParseExact("23.3.1992 00:00:00", "dd.MM.yyyy hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture);

Converting MMDDYYYY to 01-JAN-YY

I have a string in the format in = "01012012". I want it to convert to: 01-JAN-12. How do I achieve it in C#?
DateTime dateTime = DateTime.ParseExact("01012012", "MMddyyyy", CultureInfo.InvariantCulture);
string newDateString = dateTime.ToString("dd-MMM-yy");
EDIT: Changed output to 2 digit date (instead of 4)
Got my answer:
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime date = DateTime.ParseExact("08312012", "MMddyyyy", provider);
string getDate = date.ToString("dd-MMM-yy");
Thanks guys!!!

Categories

Resources