I'm trying to figure out the format of few dates that are available in numeric format. I need to convert them to exact dates. I have a sample dates that I was trying to convert:
1443506173.0 >> Sep 29, 2015
1443505895.0 >> Sep 29, 2015
1441805416.0 >> Sep 09, 2015
1438174556.0 >> Jul 29, 2015
1436476814.0 >> Jul 10, 2015
1414994162.0 >> Nov 03, 2014
1413294207.0 >> Oct 14, 2014
By looking at the first two entries, I can see that the numbers are changing but both are representing the same dates. Means there must be time embedded into this date format. Currently I'm concerned with extracting only date, I dont need to extract time at the moment. Would be great if it was simple enough to extract time as well.
Can anyone help figure this out? In case you're wondering, I got these dates from Instagram posts feed. Using WebClient I downloaded an Instagram photo's URL. This date format is there in the scripts section. If I'm able to decode this date format, I would know what the date (and time) of the photo post was at Instagram.
Thanks in advance. I'm using C# to perform this conversion.
Instagram uses the Unix Timestamp for its format (the number of seconds since 1st January 1970).
One method of converting this to a DateTime object would be:
DateTime dateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(yourUnixTimestampValue).ToLocalTime();
This is a UNIX Timestamp
void Main()
{
var timestamps = new[]{
new {stamp = 1443506173.0, datetime = new DateTime(2015, 9, 29)},
new {stamp = 1443505895.0, datetime = new DateTime(2015, 9, 29)},
new {stamp = 1413294207.0, datetime = new DateTime(2014, 10, 14)}
};
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
foreach (var i in timestamps)
{
Console.WriteLine("Item: {0}, Converted: {1}", i, new DateTime(1970, 1,1).AddSeconds(i.stamp).ToLongDateString());
}
}
Output:
Item: { stamp = 1443506173, datetime = 09/29/2015 00:00:00 }, Converted: Tuesday, 29 September 2015
Item: { stamp = 1443505895, datetime = 09/29/2015 00:00:00 }, Converted: Tuesday, 29 September 2015
Item: { stamp = 1413294207, datetime = 10/14/2014 00:00:00 }, Converted: Tuesday, 14 October 2014
Related
I have a System.DateTime which is already in a specific timezone but does not specify a DateTime.Kind, and a string that represents an IANA timezone. I want to convert this to a NodaTime.ZonedDateTime.
For example:
var original = new DateTime(2016, 1, 1, 12, 0, 0);
var timezone = "America/Chicago";
I want a ZonedDateTime that represents 01/01/2016 - 12PM in Chicago time. How do I do this?
If I understand you correctly, you want to take a standard DateTime with a known time in Chicago and get a ZonedDateTime representing this Date + Time + Time Zone, correct?
If so, I believe this will do it. The intermediate conversion from DateTime to LocalDateTime might not be necessary if you can construct it directly.
var dotNetDateTime = new DateTime(2016, 1, 1, 12, 0, 0);
var localDate = LocalDateTime.FromDateTime(dotNetDateTime);
var chicagoTime = DateTimeZoneProviders.Tzdb["America/Chicago"];
var chicagoZonedDateTime = chicagoTime.AtStrictly(localDate); // Will contain 2016 01 01 noon
// now you can convert to time in Los Angeles
var laTime = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];
var laZonedDateTime = chicagoZonedDateTime.WithZone(laTime); // will contain 2016 01 01 10am
I have a server running in New York that saves a DateTime into the database in local time.
I then have a client application running in GMT timezone that needs to save down a DateTime in the same local New York time
Can I do using:
TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"))
Or I need to consider daylight savings?
Now in my UI I need to display 2 columns. 1) the DateTime from the database and the DateTime in the database converted to GMT
How can I do this?
The TimeZoneInfo class should give you what you need.
var date = (DateTime)reader[0]; // Retrieve data from database however you normally get it
var est = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var utcTime = TimeZoneInfo.ConvertTimeToUtc(date, est);
var localTime = utcTime.ToLocalTime();
Edit: You modified your question to indicate you have looked at TimeZoneInfo, so this doesn't add much anymore. However to resolve the ambiguous time issue mentioned by Jon, take a look at the IsAmbiguousTime() and GetAmbiguousTimeOffsets() methods to be able to detect values that can't be definitively converted.
Here's a sample to show that it should work as expected. That is, it will do the DST conversion.
DateTime dt1 = new DateTime(2016, 06, 01, 08, 00, 00, DateTimeKind.Unspecified); // Daylight saving time
DateTime dt2 = new DateTime(2016, 12, 01, 08, 00, 00, DateTimeKind.Unspecified); // Standard time
var dt1Converted = TimeZoneInfo.ConvertTimeToUtc(dt1,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
var dt2Converted = TimeZoneInfo.ConvertTimeToUtc(dt2,
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
var diff1 = (dt1Converted - dt1).TotalHours;
var diff2 = (dt2Converted - dt2).TotalHours;
Console.WriteLine($"dt1: {dt1}, converted: {dt1Converted}, diff: {diff1}");
Console.WriteLine($"dt2: {dt2}, converted: {dt2Converted}, diff: {diff2}");
Output:
dt1: 2016-06-01 08:00:00, converted: 2016-06-01 12:00:00, diff: 4
dt2: 2016-12-01 08:00:00, converted: 2016-12-01 13:00:00, diff: 5
Is there any chance you can change this to use DateTimeOffset ? It was designed to resolve all of these problems
I have js calender when I select date from calender it calls C# service which takes string as parameter . It is getting date in format Mon Apr 18 2016 00:00:00 GMT 0500 . I want Monday intead of Mon.
My java Script code is
$scope.deliveryDateTime = { Day: new Date(2015, 11, 28, 14, 57) };
var DeliveryDay = $scope.deliveryDateTimeData.Day;
$http.get(meta.service.GetTimeAvlb+'?day='+ DeliveryDay).
success(function (data) {
debugger;
console.log("Data" + data);
$scope.AvailiableTimeData = data.data;
}
}).
error(function (data) {
});
Here is my C# Service code
public ApiResult GetTimeAvlb( string day )
{
var splitDay = day.Split(' ');
string dayName1 = splitDay[0].ToString();
apiresilt.data = db.TimeAvaliblities.Where(ss => ss.DayName == dayName1).ToList();
return apiresilt;
}
here dayName1 returning Mon while my db has value Monday .
If you really wanna parse this string to DateTime, you have to use your GMT 0500 (Pakistan Standard Time) part as a string literal. DateTime parsing methods does not support timezone names and UTC Offset part without TimeSeparator.
Then you can use DateTime.ToString method with dddd specifier and english-based culture like InvariantCulture.
var s = "Mon Apr 18 2016 00:00:00 GMT 0500 (Pakistan Standard Time)";
var dt = DateTime.ParseExact(s, "ddd MMM dd yyyy HH:mm:ss 'GMT 0500 (Pakistan Standard Time)'",
CultureInfo.InvariantCulture);
Console.WriteLine(dt.ToString("dddd", CultureInfo.InvariantCulture));
By the way, there is no such a thing as date time string. Your data either can be DateTime or string.
.ToString("Format-String") can be used for display date in required format;
DateTime dt = DateTime.Now; will be the current date APR/01/2016
string str = dt.ToString("dddd"); // this will be Friday for 04/01/2016
If I have a DateTime instance which represents a valid UTC time, and an offset that converts that DateTime to the time zone where it applies, how do I construct a DateTimeOffset instance to represent this?
var utcDateTime = new DateTime(2011, 02, 29, 12, 43, 0, /*DateTimeKind.Utc*/);
var localOffset = TimeSpan.FromHours(2.0);
var dto = ...
// Here the properties should be as follows;
// dto.UtcDateTime = 2011-02-29 12:43:00
// dto.LocalDateTime = 2011-02-29 14:43:00
Perhaps I'm not understanding the DateTimeOffset structure correctly, but I'm unable to get the expected output.
Thanks in advance
Looks like you want:
var utcDateTime = new DateTime(2012, 02, 29, 12, 43, 0, DateTimeKind.Utc);
var dto = new DateTimeOffset(utcDateTime).ToOffset(TimeSpan.FromHours(2));
Note that I changed the year from 2011 (which is not a leap year and does not have 29 days in February) to 2012.
Test:
Console.WriteLine("Utc = {0}, Original = {1}", dto.UtcDateTime, dto.DateTime);
Output:
Utc = 2/29/2012 12:43:00 PM, Original = 2/29/2012 2:43:00 PM
Do note that you probably don't want the LocalDateTime property, which may represent the instant in time as of the local system's timezone.
I have the data in string format
"22:48:32 Jul 29, 2011 PDT"
Now I want to convert it into datetime.
How can I do it.
You are probably after this method:
DateTime.Parse(string)
PDT is -7 from GMT, so you can refer to the GMT and subtract 7 hours.
DateTime datetime = DateTime.Parse("Jul 29 2011 22:48:32 GMT"); // notice GMT
datetime = datetime.AddHours(-7); // minus 7 hours