Posts

Showing posts with the label datetime

SQL declare datetime - 1001-01-01

Image
Clash Royale CLAN TAG #URR8PPP SQL declare datetime - 1001-01-01 I am trying to declare a datetime variable with the value 1001-01-01 00:00:00.000 I have tried the following approaches with no luck declare @d1 datetime = '1001-01-01'; declare @d2 datetime = 10010101; declare @d3 datetime = '1001-01-01 00:00:00'; declare @d4 datetime = cast ('1001-01-01' as datetime) I get the following errors Msg 242, Level 16, State 3, Line 1 The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. Msg 8115, Level 16, State 2, Line 2 Arithmetic overflow error converting expression to data type datetime. Msg 242, Level 16, State 3, Line 3 The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. Msg 242, Level 16, State 3, Line 4 The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. Is it possible to declare a datetime variable which can hold the value 1001-...

How to get Date Time format '2018-10-26T16:44:38+01:00' in C#

Image
Clash Royale CLAN TAG #URR8PPP How to get Date Time format '2018-10-26T16:44:38+01:00' in C# How can i get date time format as "2018-10-26T16:44:38+01:00" "2018-10-26T16:44:38+01:00" I have tried using DateTimeOffset.UtcNow.ToString("o") but its different. DateTimeOffset.UtcNow.ToString("o") Can anyone suggest. Have you this format DateTime.Now.ToString("yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK"). c-sharpcorner.com/blogs/… – Faizan Fayaz 18 mins ago 2 Answers 2 Feel free to look at this reference on the strings available; to answer your question as straightforward as you asked it: string time = DateTime.UtcNow.ToLocalTime().ToString("yyyy-MM-...

How to access dictionary when index is datetime?

Image
Clash Royale CLAN TAG #URR8PPP How to access dictionary when index is datetime? I have multiple csv-files each containing two columns. The first contains the timestamp and the second column contains a measurement value. Lets assume the csv-file looks like this: Date,Temperature (Celsius) 2018-07-24T20:45:31,28.86 2018-07-24T20:35:31,29.06 2018-07-24T20:25:31,29.19 2018-07-24T20:15:32,29.31 2018-07-24T20:05:31,29.48 2018-07-24T19:55:31,29.58 2018-07-24T19:45:31,29.82 2018-07-24T19:35:32,30.32 2018-07-24T19:25:31,31.00 and I import it like this: df = pd.read_csv(csv-file, sep=',', header = 0, usecols=[0, 1], parse_dates=[0], infer_datetime_format=True) dd = df.set_index('Date').T.to_dict() I thought this would be convenient later, when I would search for the same key (same timestamp) in multiple files in order to merge them. However I cannot access the dictionary. The keys are recognised as timestamp. dd.keys() dict_keys([Timestamp('2018-07-24 20:45:31'), Timestam...

Datetime returns wrong time in unix

Image
Clash Royale CLAN TAG #URR8PPP Datetime returns wrong time in unix I have a wired situation here with returning the time in unix format. I have this function: def get_today_date(): nowDate = str(datetime.datetime.utcnow().date()) unix_date = time.mktime(datetime.datetime.strptime(nowDate, "%Y-%m-%d").timetuple()) return (unix_date) it's straightforward when I print the value of "nowDate" -> it gives me the right value I want (ex: 7/29/2018 00:00:00 UTC) which is correct. But when I change the format from Unix timestamp to date format and get only the date without time, It gives me (7/28/2018). I've took the value in unix and checked it, it gives me (7/28/2018 21:00:00). Why? My laptop is in a UTC +3 timezone. Did something go wrong in the conversion? Or its an internal error in my laptop makes the output wrong? Some of the data time functions take timezone into account and some don't. This is where you probably mad...

Daylight Savings Timezone Detection From Strings In JavaScript

Image
Clash Royale CLAN TAG #URR8PPP Daylight Savings Timezone Detection From Strings In JavaScript I've looked in several places but I could not find a solution to this problem. I have some data I'm trying to import and for my case, it contains strings representing dates which are in the Eastern Time Zone (ET). Strings look like this: July 28, 2018 03:39 PM If I try to create a new date object in JavaScript, I can create the new date object, but the problem is that there is no ET timezone, so I can create it either using EST (Eastern Standard Time) or EDT (Eastern Daylight Time). ET EST EDT console.log(new Date('July 28, 2018 03:39 PM EST')); 2018-07-28T19:39:00.000Z Or console.log(new Date('July 28, 2018 03:39 PM EDT')); 2018-07-28T20:39:00.000Z We can clearly see there is one hour difference between both. The rules to determine if Daylight Saving is active or not seems quite complex and can vary from geographic region. So my question is: is there any solution ou...

How to exclude weekends between two date ranges

Image
Clash Royale CLAN TAG #URR8PPP How to exclude weekends between two date ranges I have a FromDate and ToDate and days interval between them. Now i want to get all date excluding weekends If there is any weekends between my FromDate and ToDate,then this date will be increment by 2 days saturday and sunday. In below example, i have calculated all date between 7/27/2018 and 8/27/2018 with 5 days interval between them, public function testdate(){ $date_from = new DateTime("7/27/2018"); $date_to = new DateTime("8/27/2018"); $interval = new DateInterval("P5D"); $dates = new DatePeriod($date_from, $interval, $date_to); $out = array(); if (!empty($dates)) { foreach($dates as $dt) { $out = array( 'month_year' => $dt->format('d/m/Y') ); } } '<pre...

How to get difference between two string dates

Image
Clash Royale CLAN TAG #URR8PPP How to get difference between two string dates There are two dates which are in string formats. String date1 = Thu Jul 26 13:45:49 IST 2018 String date2 = Wed Feb 26 17:31:27 IST 2016 How to get difference between these two dates? Possible duplicate of Calculating the difference between two Java date instances – Draken 9 mins ago @Draken Actually, that link says what to do when you already have some sort of date object representation. I'd be curious to see an answer which does the parsing the Java 8 way. – Tim Biegeleisen 7 mins ago Possible duplicate of compare dates in String format – LuCio 6 mi...

Convert ISO date string to Timestamp

Image
Clash Royale CLAN TAG #URR8PPP Convert ISO date string to Timestamp String startDate = "2018-07-29T09:50:49+05:30"; String TAG = "Extra"; final String TIMESTAMP_FORMATE = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; DateFormat df = new SimpleDateFormat(TIMESTAMP_FORMATE); try { Date date = df.parse(startDate); System.out.println(TAG + "Start: " + date.getTime()); System.out.println(TAG + "Start: " + date.getDate()); System.out.println(TAG + "Start: " + date.getHours() + ":" + date.getTime()); } catch (ParseException e) { e.printStackTrace(); } Its giving an error java.text.ParseException: Unparseable date: "2018-07-29T09:50:49+05:30" java.text.ParseException: Unparseable date: "2018-07-29T09:50:49+05:30" Any idea what I am missing here? stackoverflow.com/questi...

fixed offset specifications of a GMT timestamp

Image
Clash Royale CLAN TAG #URR8PPP fixed offset specifications of a GMT timestamp My data is stored with a timestamp in Greenwich Mean Time (GMT): import pandas as pd import pytz # data is stored in UTC timestamp_utc = pd.Timestamp('2018-1-18 23:00', tz='Etc/GMT') print(timestamp_utc) 2018-01-18 23:00:00+00:00 Now I would like to view my data in Central European Time (CET), which has a fixed offset of +1 hour to GMT. # Central European Time (CET) cet_tz = pytz.timezone('Etc/GMT+1') timestamp_cet = timestamp_utc.astimezone(cet_tz) print(timestamp_cet) 2018-01-18 22:00:00-01:00 This is very confusing for me, I would have expected 2018-01-19 00:00:00+01:00 . 2018-01-19 00:00:00+01:00 In the documentation the following is mentioned on fixed offsets: Fixed offsets The 'Etc/GMT*' time zones mentioned above provide fixed offset specifications, but watch out for the counter-intuitive sign convention. Can anybody explain what this means? Does it really mean, tha...

How to convert int64 to datetime in pandas

Image
Clash Royale CLAN TAG #URR8PPP How to convert int64 to datetime in pandas I have a pandas dataframe that has a column of type int64 but this columns represets date, e.g. 20180501. I'd like to convert this column to datetime and I'm having the following code but it returns an error message df['new_date'] = pd.to_datetime(df['old_date'].astype('str'), format = '%y%m%d') I'm getting the following error message ValueError: unconverted data remains: 0501 How can I fix my code? 1 Answer 1 You need a capital Y . See Python's strftime directives for a complete reference. Y df = pd.DataFrame({'old_date': [20180501, 20181230, 20181001]}) df['new_date'] = pd.to_datetime(df['old_date'].astype(str), format='%Y%m%d') print(df) old_date new_date 0 20180501 2018-05-01 1 20181230 2018-12-30 2 20181001 2018-10-01 ...