Change ISO date and time to indian time zone in C# - c#

How can I convert a date in the following format to a date in the India time zone (UTC+5:30) using C#?
2012-09-13T05:08:03.151Z

How about
DateTime dt = DateTime.ParseExact("2012-09-13T05:08:03.151Z",
"yyyy-MM-dd HH:mm:ssK",
CultureInfo.InvariantCulture)
Then
var indianTime = TimeZoneInfo.ConvertTime (dt,
TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));

Try this:
DateTime dt = DateTime.ParseExact("your current date string","your current date string format",null);
string IndianDT = dt.ToString("dd/MM/yyy");
Now in your IndianDT string you will have your Date desired format.
Edit:
In my above code:
replace "your current date string fromat" with "yyyy-MM-ddThh:mm:ss.fffZ"

Related

How to Change Time In PM Or AM in 24 Hour format

how to Change Time According to PM OR AM please help me this in C#
string time = "0610"; OR "1200" OR "0100" OR "1430"
DateTime dt = DateTime.Parse(time);
i Need like this : 06:10 AM
You want to use ParseExact to parse the string into a date/time:
string time = "0610";
DateTime dt = DateTime.ParseExact(time, "HHmm"", CultureInfo.InvariantCulture);
// HH means 24-hour format with leading zero
Note that DateTime does not HAVE a format, it just represents a point in time. If you then want to display it with the AM/PM indicator, you could use ToString, or set the format specifier in the display control (if applicable):
string timeAmPm = dt.ToString("hh:mm tt");
Try this
//date is your datetime
DateTime date = DateTime.Now;
string formatTime = date.ToString("hh:mm tt", CultureInfo.InvariantCulture);
Console.WriteLine(formatTime);
See more:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings#ttSpecifier

convert "23/11/2016" date into "MM/dd/yyyy" format in C#

string valid= "22/11/2016";
DateTime date = DateTime.ParseExact(valid, "MM/dd/yyyy", null);//not valid date time format
when i am trying to convert date in "MM/dd/yy" format it gives error that inputted string not in valid date time format.Please help
format should be "dd/MM/yyyy",note that d for date and M for month, 22 can't be a month
string valid= "22/11/2016";
DateTime date = DateTime.ParseExact(valid, "dd/MM/yyyy", null);//
You can try this
string valid = "22/11/2016"; // should be in this format dd/MM/yyyy
DateTime date = Convert.ToDateTime(valid);

DateTime Parse from PDT

I have a string which contains a date as shown below
string dateTime = "18-Aug-2016 12:02:44 AM PDT";
I want it to be converted to the below format
Output = 2016-08-18T00:02:44-07:00
I tried the below code, but still I need to modify it to get my required output
string mydate = dateTime.Replace("PDT", "-0700");
DateTime dt = Convert.ToDateTime(mydate);
string dateTime = "18-Aug-2016 12:02:44 AM PDT";
string mydate = dateTime.Replace("PDT", "-0700");
DateTime dt = Convert.ToDateTime(mydate);
Console.WriteLine("The current date and time: {0:O}", dt);
Gives me the following output:
The current date and time: 2016-08-18T09:02:44.0000000+02:00
Not sure if PDT is globally unique, nor if that zone changes during daylight savings time.
Id report back to the Google server team that they are returning something "interesting"

How to change the string into datetime format in c# asp.net

i have a string which is "26052014153850" it is in the format of date month year hour minute second. i want to convert this to date and time format but its showing string is not recognized as valid date and time
string result ="26052014153850";
DateTime dt = DateTime.ParseExact(result, "dd/MM/yyyy/HH/mm/ss", CultureInfo.InvariantCulture);
DateTime dt = DateTime.ParseExact
(result, "ddMMyyyyHHmmss", CultureInfo.InvariantCulture);
The format of the string representation must match the specified format exactly.
Read this for more from MSDN.

DateTime error in C#

When I enter the date in the below format then my db accepts it
22/06/2011 00:00:00
But when I enter the date in this format
mm/dd/yyyy 00:00:00
then my DB throws an error saying DateTime not recognised. My calendar gives me DateTime in mm/dd/yyyy hh:mm:ss format. How can I change that in my code to dd/mm/yyyy?
DateTime Res_date = Convert.ToDateTime(txt_DT.Text);
param[5] = new MySqlParameter("#RespondBy", MySqlDbType.DateTime);
param[5].Value = Res_date;
command.Parameters.AddWithValue("#RespondBy", Res_date);
The DateTime is entered in the txt_DT.text textbox.
How can I convert the date and then convert the string to DateTime?
I think you should use the DateTime.ParseExact method. Msdn Documentation http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx
DateTime Res_date = DateTime.ParseExact(txt_DT.Text, "MM/dd/yy hh:mm:ss", ... );
Then you can use String.Format to convert your DateTime to any format you want.
http://www.csharp-examples.net/string-format-datetime/

Categories

Resources