string date = "2017-01-05T00:00:00+00:00";
string time = Convert.ToDateTime(date).ToString("MM/dd/yyyy");
I am getting the above date from my client database, i am trying to convert that time to string but when i executed it was returning 01/04/2017 . why this is converting one day before ?
Ok my bad. Here goes:
Yes I agree its a timezone offset issue, but this will work anyways:
string date = "2017-01-05T00:00:00+00:00";
string time = DateTimeOffset.Parse(date).DateTime.ToString("MM/dd/yyyy");
Related
I have a database connected to c# windows forms application. I want to get a current date and time from database, and then to use that date and time to insert some other data into database.
When I execute a query select now() from WAMP's mySqlConsole I get what I expect: 12-04-17 12:06:28, but when I run that same query from c# and store the value to string like this: String datetime = cmdDatetime.ExecuteScalar().ToString(); the month becomes Apr in which format i cant insert into db.
Is there a way for c# to store mysql datetime in the same format as it is in mysql?
You can use the DateTime object if possible, or else you will need to convert the date time received to the format you want:
DateTime dateTime = (DateTime)cmdDatetime.ExecuteScalar();
string dateTimeString = dateTime.ToString(CultureInfo.InvariantCulture);
Using the InvariantCulture, you will always get the results in the same way, namely in the en-US settings.
Additionally, you can force the specific format too:
string dateTimeString = dateTime.ToString("dd-MM-yyyy HH:mm:ss");
You have to specify the format for example:
DateTime datet = (DateTime)cmdDatetime.ExecuteScalar();
String datetime = datet.ExecuteScalar().ToString( "dd-mm-yyy HH:mm:ss");
Try That:
DateTime datetime = (DateTime)cmdDatetime.ExecuteScalar();
String datetimeStr = datetime.ToString("yyyyMMddHHmmss");
I have console application that accepts date as parameter. However, the date is passed as a string in this format:
string dt = DateTime.Now.ToString("yyyyMMdd");
Once the date is entered I need to programmatically get day - 1 from the entered date. Since this is a string, I cannot do any calculation.
For example, user enters:
20141023
I need to subtract a day from the date to get:
20141022
I did a quick fix to solve my immediate need, however, this is not the right way to do it and it has a bug:
int yt = Int32.Parse(dt) - 1;
And then I turn around and convert it yt.ToString()
The above solution will not work if it's the 1st of the month.
Is there a way I can programmatically get yesterday's date in the format (yyyyMMdd) without changing the format and possibly not using the TimeSpan?
Why don't parse the input into a DateTime object? Then you can use the DateTime.AddDays(-1)
For example:
var inputDate = DateTime.ParseExact("20141022", "yyyyMMdd", CultureInfo.InvariantCulture); // change "20141022" into the inputted value
var yesterday = inputDate.AddDays(-1);
var yesterdayString = yesterday.ToString("yyyyMMdd"); // this will be yesterdays date, in the string format
Try this...
DateTime data = DateTime.ParseExact("20141023", "yyyyMMdd", CultureInfo.InvariantCulture);
Console.WriteLine("{0} - {1}", data, data.AddDays(-1).ToString("yyyyMMdd"));
Would this work for you ?
string newDateTimeStr = (DateTime.Today.AddDays(-1)).ToString("yyyyMMdd");
EDIT:
for the date entered by the user:
string txtInputDate = Console.ReadLine();
DateTime dateTime = new DateTime(txtInputDate).ToLocalTime();
string newDateTimeStr = (dateTime.AddDays(-1)).ToString("yyyyMMdd");
This is small code. But I couldn't find whats wrong with it. In my application I want to get the current month in long month format(ex:January). I used the following two lines of code.
DateTime now = DateTime.Now;
string month = now.Month.ToString("MMMM",CultureInfo.CurrentCulture);
but its return "MMMM" for the values of month. Can anybody tell me whats wrong in this code.
now.Month is the int representation the month e.g. 1 for January, that's why the .ToString("MMMM") works "strange".
What you need is to call the .ToString() directly on the DateTime object:
DateTime now = DateTime.Now;
string month = now.ToString("MMMM",CultureInfo.CurrentCulture);
String month =
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(
DateTime.Now.Month);
I want to retrieve yesterday's date in my ASP.NET web application using C#.
I've tried searching for a solution but have not had much success. The code I'm using just outputs today's date:
string yr = DateTime.Today.Year.ToString();
string mn = DateTime.Today.Month.ToString();
string dt = DateTime.Today.Day.ToString();
date = string.Format("{0}-{1}-{2}", yr, mn, dt);
How can I get yesterday's date?
Use DateTime.AddDays() method with value of -1
var yesterday = DateTime.Today.AddDays(-1);
That will give you : {6/28/2012 12:00:00 AM}
You can also use
DateTime.Now.AddDays(-1)
That will give you previous date with the current time e.g. {6/28/2012 10:30:32 AM}
The code you posted is wrong.
You shouldn't make multiple calls to DateTime.Today. If you happen to run that code just as the date changes you could get completely wrong results. For example if you ran it on December 31st 2011 you might get "2011-1-1".
Use a single call to DateTime.Today then use ToString with an appropriate format string to format the date as you desire.
string result = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
You don't need to call DateTime.Today multiple times, just use it single time and format the date object in your desire format.. like that
string result = DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");
OR
string result = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
You will get yesterday date by this following code snippet.
DateTime dtYesterday = DateTime.Now.Date.AddDays(-1);
var yesterday = DateTime.Now.AddDays(-1);
Something like this should work
var yesterday = DateTime.Now.Date.AddDays(-1);
DateTime.Now gives you the current date and time.
If your looking to remove the the time element then adding .Date constrains it to the date only ie time is 00:00:00.
Finally .AddDays(-1) removes 1 day to give you yesterday.
string result = DateTime.Now.Date.AddDays(-1).ToString("yyyy-MM-dd");
DateTime dateTime = DateTime.Now ;
string today = dateTime.DayOfWeek.ToString();
string yesterday = dateTime.AddDays(-1).DayOfWeek.ToString(); //Fetch day i.e. Mon, Tues
string result = dateTime.AddDays(-1).ToString("yyyy-MM-dd");
The above snippet will work. It is also advisable to make single instance of DateTime.Now;
DateTime.Today as it implies is todays date and you need to get the Date a day before so you subtract one day using AddDays(-1);
There are sufficient options available in DateTime to get the formatting like ToShortDateString depending on your culture and you have no need to concatenate them individually.
Also you can have a desirable format in the .ToString() version of the DateTime instance
I am working with DateTime, trying to get date in the format like this:
03.05.2010-04.05.23 that is: dd.MM.yyyy-HH.mm.ss
I'm using DateTime.ParseExact to try to achieve this (maybe wrong)
So far I have:
var dateInFormat = DateTime.ParseExact(DateTime.Now.ToShortDateString(), "dd.MM.yyyy.HH.mm.ss", null);
But can't quite get exactly what I want. Basically I want to keep the 0, for example time is 05:03:20 PM I don't want it to show like 5:3:20 PM
Any ideas?
If you have a date as DateTime (not string) then you can format it as leppie indicates:
string date = DateTime.Now.ToString("dd.MM.yyyy-HH.mm.ss");
If on the other hand you have a date as string in the format dd.MM.yyyy-HH.mm.ss then you can use
string mydate = "03.05.2010-04.05.23";
DateTime dateFromString = DateTime.ParseExact(mydate, "dd.MM.yyyy-HH.mm.ss", null);
you get the exception because your string has the wrong format (namely the systems short date format)-
Is there any problem just using ToString()? Eg:
Console.WriteLine(dateInFormat.ToString("dd.MM.yyyy-HH.mm.ss"));