Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting python to c#
#1
Hello,
I am new to python and having a hard time to understand parts of the code from dateutil.relativedelta. Can someone please help me to translate the code below to c#? Thanks in advance

def __init__(self, dt1=None, dt2=None, years=0, months=0, days=0, leapdays=0, weeks=0, hours=0, minutes=0, seconds=0, microseconds=0, year=None, month=None, day=None, weekday=None, yearday=None, nlyearday=None, hour=None, minute=None, second=None, microsecond=None): if dt1 and dt2: # datetime is a subclass of date. So both must be date if not (isinstance(dt1, datetime.date) and isinstance(dt2, datetime.date)): raise TypeError("relativedelta only diffs datetime/date") # We allow two dates, or two datetimes, so we coerce them to be # of the same type if (isinstance(dt1, datetime.datetime) != isinstance(dt2, datetime.datetime)): if not isinstance(dt1, datetime.datetime): dt1 = datetime.datetime.fromordinal(dt1.toordinal()) elif not isinstance(dt2, datetime.datetime): dt2 = datetime.datetime.fromordinal(dt2.toordinal()) self.years = 0 self.months = 0 self.days = 0 self.leapdays = 0 self.hours = 0 self.minutes = 0 self.seconds = 0 self.microseconds = 0 self.year = None self.month = None self.day = None self.weekday = None self.hour = None self.minute = None self.second = None self.microsecond = None self._has_time = 0 # Get year / month delta between the two months = (dt1.year - dt2.year) * 12 + (dt1.month - dt2.month) self._set_months(months) # Remove the year/month delta so the timedelta is just well-defined # time units (seconds, days and microseconds) dtm = self.__radd__(dt2) # If we've overshot our target, make an adjustment if dt1 < dt2: compare = operator.gt increment = 1 else: compare = operator.lt increment = -1 while compare(dt1, dtm): months += increment self._set_months(months) dtm = self.__radd__(dt2) # Get the timedelta between the "months-adjusted" date and dt1 delta = dt1 - dtm self.seconds = delta.seconds + delta.days * 86400 self.microseconds = delta.microseconds
Reply
#2
You may find better luck in a C# forum.

That said, when people come here asking for help converting their other language code to Python, they usually don't get much help because they post a big block of code with no effort from them. I recommend you try this on your own, and ask as specific a question as possible when you get stuck.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Converting python to FileMaker DWolf 6 4,087 Sep-22-2022, 03:40 AM
Last Post: DWolf
  converting array to and from string in python 3.7.2 srm 5 8,992 Jul-03-2019, 01:11 PM
Last Post: snippsat
  help with converting C# function to Python korenron 17 16,326 May-19-2019, 10:26 AM
Last Post: Gribouillis
  Converting R code to python mcva 2 14,525 Mar-09-2019, 04:01 PM
Last Post: mcva
  converting from c# to python peper 1 4,081 Jun-12-2018, 05:19 PM
Last Post: micseydel
  converting python script to another version gptura 5 8,841 Mar-13-2018, 12:16 PM
Last Post: gptura
  Converting Pseudocode to Python ncp511 1 6,612 Jan-16-2018, 04:34 PM
Last Post: Windspar
  Resources for converting Python 2.x code to 3.x Luke_Drillbrain 4 7,013 May-03-2017, 06:34 PM
Last Post: snippsat
  Need help converting some code from python 2.7 to 3.5 Blue Dog 3 6,517 Oct-18-2016, 12:30 AM
Last Post: sparkz_alot

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.