What does this datetime mean? - datetime-format

Could someone tell me what this datetime means?
2014-10-12T21:48:42-04:00
Why is there a T in the middle?
Why is there a -04:00 at the end? Does it represent the time between 2014-10-12T21:48:42 and 4 hours before that time (or 4 minutes)?

That is a timestamp in ISO 8601 format:
http://en.wikipedia.org/wiki/ISO_8601
Why is there a "T" in the middle?
That is the symbol separating the date from the time.
why is there a -04:00 at the end? Does it represent the time between
2014-10-12T21:48:42 and 4 hours before that time (or 4 minutes)?
That indicates the time zone offset from UTC

This is the ISO 8601 format used by the W3C.
That date time means October 12, 2014 at 21:48:42 at a timezone -4 hours from UTC.

Related

angularjs Date Daylightsaving issue

I am using date to display my date on html like:
{{updateDate| date: 'dd/MM/yyyy HH:mm:ss'}}
Dates are all saved in UTC. The problem is it displays the date in locale timezone but not considering Daylight saving on/off. As in for BST it always shows +1 hr from UTC.
I want it to also consider DST(daylight saving time).
Any help, please.
{{ date_expression | date : format : timezone}}
As per angular date filter documentation:
date: Here date can be Date Object, milliseconds or ISO 8601 datetime string formats (like: e.g. yyyy-MM-ddTHH:mm:ss.sssZ). Here Z is 4 digit (+sign) representation of the timezone offset (-1200-+1200). If no timezone is specified in the string input, the time is considered to be in the local timezone.
format: this is optional, If not specified, mediumDate(equivalent to 'MMM d, y' for en_US locale (e.g. Sep 3, 2010)) is used.
timezone: Timezone to be used for formatting. It understands UTC/GMT and the continental US time zone abbreviations, but for general use, use a time zone offset, for example, '+0430' (4 hours, 30 minutes east of the Greenwich meridian) If not specified, the timezone of the browser will be used.
This may help you.

Azure SQL display local time

I'm trying to display datetime values in local time. By default, Azure SQL Database stores dates and times in UTC, there is no way around this. (This was a pain when migrating from on premise SQL Server.) I would like to display a stored time value in Central European time.
Now the local time is 11:30 (CET). UTC time is 10:30.
DECLARE #TestTime DATETIME;
SET #TestTime = '2016-11-02 10:30:00'
SELECT #TestTime
--Returns 2016-11-02 10:30:00
SELECT #TestTime AT TIME ZONE 'Central European Standard Time'
--Returns 02 November 2016 10:30:00 +01:00
I need to return 2016-11-02 11:30:00 somehow. Now for the fun part:
As has been suggested here:
SELECT convert(DATETIME,#TestTime AT TIME ZONE 'Central European Standard Time',1)
--Returns 2016-11-02 09:30:00 So instead of adding the timezonedifference it subtracts it.
This works, but makes me sick:
SELECT DATEADD(MINUTE,DATEPART(tz,#TestTime AT TIME ZONE 'Central European Standard Time'),#TestTime)
--Returns 2016-11-02 11:30:00
A similar solution has been suggested here, but it plays with string operations. My suspicion is that something is wrong in AT TIME ZONE; it should have displayed 11:30 +1, and not 10:30 +1, no?
Is there really no proper way to display a UTC time in local time? This "hacking around it" feels awfully dirty, especially since at any point in time it just might stop working (e.g. Microsoft fixes / introduces a bug).
Thanks!
The AT TIME ZONE statement performs two distinct operations:
To assert that a datetime (or datetime2) is in a particular time zone, thus looking up the correct offset for that zone and returning a datetimeoffset value with the correct offset applied.
To convert a datetimeoffset value to a different time zone, using the offset from the source value to pin down an exact point in time, then looking up the new offset for the requested time zone and applying it. This returns a datetimeoffset with a potentially different local time and offset than the original, but representing the same moment in time.
You are using the first part only. To convert a datetime from UTC to a specific time zone, you'll need to use both.
SELECT #TestTime AT TIME ZONE 'UTC' AT TIME ZONE 'Central European Standard Time'
The first AT TIME ZONE asserts that the input datetime is in UTC, resulting in a datetimeoffset that has +00:00 for the offset. The second AT TIME ZONE converts that to the time zone requested.

Convert String to datetime in SQL Server 2008

How will I convert date in the format Sat Mar 29 00:00:00 EST 1975 to datetime in TSQL?
I get the date in this format from an old table which defined the date of birth column as NVARCHAR and stores the data as Mon Jun dd hh:mm:ss GMT yyyy format. I need to read another table which has the dob in datetime using this value.
So basically I want to convert, say Sat Mar 29 00:00:00 EST 1975 to 1975-03-29 00:00:00.000
Is there a way in T-SQL to do this conversion? I tried the CONVERT function, but I am unable to locate the correct 'style' to use.
Examining the data format, it appears to be a fixed length string.
The first portion is the day of week, which can be discarded as it isn't needed for parsing. Next you have the month and day information, which we need. After that is the time, which can be retained or discarded depending on whether you want a date or datetime as output.
Since you are looking for a date of birth, the time zone information can most likely be safely discarded.
Finally, there is the year.
If we eliminate the day of week and the time zone, sql server will parse the rest of the string with no problem.
I recommend cast(substring(#difficultTime,5,7) + substring(#difficultTime,25,4) as date), where #difficulteTime is the column name you are converting.
If you wanted to retain the time information, the following format will work cast(substring(#difficultTime,5,16) + substring(#difficultTime,25,4) as datetime)
This assumes that your strings will be of a fixed length. The first conversion shown eliminates the day of week, the time, and the time zone from the string, leaving a parseable date.
The second conversion eliminates the day of week and the time zone, leaving a parseable datetime.

Is date/time part of SQL Server's datetimeoffset UTC or local?

According to Microsoft's page on the datetimeoffset data type (see here):
A time zone offset specifies the zone offset from UTC for a time or datetime value.
But nowhere does it say whether the datetime part of the datetimeoffset string literal format is showing either:
UTC time, with the timezone offset being what to apply to that time to get to localtime, or;
localtime, with the timezone offset being what to apply to that time to get back to UTC.
This is the string literal format for datetimeoffset: YYYY-MM-DD hh:mm:ss[.nnnnnnn] [{+|-}hh:mm].
My question therefore is, is the YYYY-MM-DD hh:mm:ss[.nnnnnnn] bit of that string literal in localtime or in UTC?
The YYY-MM-DD hh:mm:ss[.nnnnnnn] [{+|-}hh:mm] format is an ISO8601 format. The ISO page gives
Coordinated universal time (UTC)
Local time with offset to UTC
As this format has the offset then the base time is local. See wikipedia for other examples.

how to interpret this timestamp?

Lets say this is the time-stamp: 2011-07-06T00:00:35.851-07:00
What does that tell me? This is how I am trying to understand it:
2011-07-06 - date
00:00:35 - hh:mm:ss
851 - micro seconds??
07:00 - what does that tell me?
I need to convert this to UTC if possible with C.
Edit 0: Thanks for the responses by #RichieHindle and #Marc B. I now understand the GMT offset.
My problem now is, I am not getting correct value out of getdate.
It says it's July 6th, 2011, 35.851 seconds past midnight, in the GMT-7 time zone. To convert to UTC (GMT-0 timezone), you'd need to add 7 hours (-7 + 7 = 0), making it 2011-07-06T07:00:35.851-00:00
851 is milliseconds (thousandths of a second) and -07:00 is the timezone (UTC minus 7 hours).

Resources