I am working on a project that requires me to be comparing the date stored as string in database to the current date.
This particular application works in localhost but on live server, it seems to be picking the date of the server. I am resident in West Central Africa. How do i get the Date returned by my converted timezone in the format dd/MM/yyyy and not MM/dd/yyyy that it is seeing it.
Below is a code snippet.
var date = DateTime.UtcNow ;
var zone = TimeZoneInfo.FindSystemTimeZoneById("W. Central Africa Standard Time");
DateTime currentTime = TimeZoneInfo.ConvertTimeFromUtc(date, zone);
var loanDates = currentTime.Date;
var OldloanDate = loanDates.ToShortDateString();
var nloanDate = DateTime.ParseExact(OldloanDate, "dd/MM/yyyy", CultureInfo.InvariantCulture).Date;
var loanDate = nloanDate.ToShortDateString();
I want the loanDate to be in string form and of the format dd/MM/yyyy
You can try this:
var date = DateTime.UtcNow;
var zone = TimeZoneInfo.FindSystemTimeZoneById("W. Central Africa Standard Time");
var currentTime = TimeZoneInfo.ConvertTimeFromUtc(date, zone);
var loanDate = currentTime.Date.ToString("dd/MM/yyyy").Replace("-", "/");
Related
I'm struggling with a problem. i have a string date looks like this, "2015-05-02 01:00:00", extracted from a database.
I know that it's british time, but my local time is belgian time.
I'm trying to store the date in UTC and in (CEST or CET depend of the season), converting it from the British time i've extract.
I tried to set Kind property to British time, but the result seems to be in local or utc time. So, i can do half of the job, but not the rest (e.g. I still need the CEST/CET time).
I tried to use this :
string dateString = (string) line["stringDate"];
DateTime ukTime = DateTime.Parse(dateString, new CultureInfo("en-GB", false));
DateTime belgianTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(ukTime, "Romance Standard Time");
The result is the same for both ukTime and belgianTime: 2015-05-02 01:00:00 with kind = unspecified.
It should be 2015-05-02 02:00:00 for belgianTime
If you just add the source time zone to the conversion method it gives the desired answer, even without specifying the IFormatProvider.
string dateString = (string) line["stringDate"];
DateTime ukTime = DateTime.Parse(dateString);
DateTime belgianTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(ukTime,
"GMT Standard Time",
"Romance Standard Time");
This gives time Kind == Unspecified. However if you use:
var belgianTime = TimeZoneInfo.ConvertTime(ukTime,
TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"),
TimeZoneInfo.Local);
for the conversion, you get Kind == Local
Use the following line of code.
var time = DateTime.Parse(DateTime.Now.ToString());
var clientZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");
var utcTime = TimeZoneInfo.ConvertTimeToUtc(time, clientZone);
You have to use the ConvertTime method. ConvertTime
a quick sample -
string s = "2015-05-02 01:00:00";
var dt = new DateTime(2015, 05, 02, 1, 0, 0);
var t = TimeZoneInfo.ConvertTime(dt, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"),
TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"));
I have datetime string
dateStr = "2017-03-21T23:00:00.000Z";
then I am calling
var date = DateTime.Parse(dateStr);
and unexpectedly my date equals
22.03.2017 00:00:00
I expected it to be 21.03.2017
What's going on here?
DateTime.Parse() is locale specific and will take into account your local time zone when parsing dates.
If you are in CET (Central European Time) during the winter your offset is one hour ahead of UTC. The date given is marked with a Z indicating it is in UTC, so DateTime.Parse() will adjust that to your local timezone.
There is an override that allows you to change that behaviour if you want, by specifying a specific DateTimeStyles enum. DateTimeStyles.AdjustToUniversal is what you are looking for as that should keep the DateTime as UTC.
And if you only want the date part afterwards, you can just call .Date on the DateTime object you got back from Parse()
So, something like this:
var date = DateTime.Parse(dateStr, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal).Date;
if the date format does not change then you can use the below code to get date part from date string. But it is a bit risky due to its strict dependency on the input format.
string dateStr = "2017-03-21T23:00:00.000Z";
int year = Int32.Parse(dateStr.Substring(0, 4));
int month = Int32.Parse(dateStr.Substring(5, 2));
int day = Int32.Parse(dateStr.Substring(8, 2));
var date = new DateTime(year, month, day);
Console.WriteLine(date);
Because the format of type 'DateTime' variable is 'YYYY-MM-DD hh:mm:ss'.
If you run this code:
var dt = DateTime.Now;
Console.WriteLine(dt);
You'll see '24/03/2017 12:54:47'
If you have 'YYYY-MM-DD' format, add .ToString("dd-MM-yyyy"), then:
string dateStr = "2017-03-21T23:00:00.000Z";
var date = DateTime.Parse(dateStr).ToString("dd-MM-yyyy");
Result:'24-03-2017'
I have a TimeZone received from customer and I use it to set expiration DateTime so that it is UTC equivalent of "End of Month" in customer's time zone.
I tried this:
var current = timezone.ToUniversalTime(DateTime.UtcNow);
But could not manage to work. Can anybody help me with that?
You can try like this:
DateTime utcTime = new DateTime(2016,2,10,10,15,00);
var tz = TimeZoneInfo.FindSystemTimeZoneById("Your Time Zone");
var tzTime = TimeZoneInfo.ConvertTimeFromUtc(utcTime, tz);
//To get time in UTC
var utcTime = TimeZoneInfo.ConvertTimeToUtc(tzTime, tz);
I want to convert 13/MAR/2015 11:26:26 GMT -6.00 to UTC. I know how to convert ,if it is in some standard time,i have converted it using following code.
string dateTime = "13/MAR/2015 11:26:26";
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Cental standard Time");
TimeZoneInfo.ConvertTimeToUtc(Convert.ToDateTime(dateTime), zone);
But i want to convert 13/MAR/2015 11:26:26 GMT -6.00 to UTC.Please help me.
The problem with parsing that string is that the time zone name is not recognised, and the offset is in the format -6.00 instead of -6:00.
Remove the time zone name, and change the offset format, and you can parse the date, then you can use the ToUniversalTime method to convert it to UTC:
DateTime t = DateTime.Parse(dateTime.Replace(" GMT ", " ").Replace(".", ":"));
DateTime utc = t.ToUniversalTime();
You can use DateTime.ToUniversalTime Method
For example
string dateTime = "13/MAR/2015 11:26:26";
TimeZoneInfo zone = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
var local = TimeZoneInfo.ConvertTimeToUtc(Convert.ToDateTime(dateTime), zone);
var offcet = zone.GetUtcOffset(local);
string output = local + " GMT " + offcet.Hours;
GMT is equal to UTC (though not the same exact thing) so you can replace the GMT in the string with UTC and parse the timezone from there.
You cannot do this in one go. The problem is that the format of your date time strings contains a timezone offset component in an unsupported format: -6.00 instead of -06:00. If that was not the case, you could directly parse your string in the following manner:
var myDateTime = "13/MAR/2015 11:26:26 GMT -06:00";
var fmt = "dd/MMM/yyyy HH:mm:ss 'GMT' zzz";
var dto = DateTimeOffset.ParseExact(myDateTime, fmt, CultureInfo.InvariantCulture);
var utcTime = dto.UtcDateTime;
You have to hack it by doing:
var myDateTime = "13/MAR/2015 11:26:26 GMT -6.00";
var fmt = "dd/MMM/yyyy HH:mm:ss 'GMT' zzz";
var dto = DateTimeOffset.ParseExact(myDateTime.Replace(".", ":")), fmt, CultureInfo.InvariantCulture);
var utcTime = dto.UtcDateTime;
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"