Identify alternate dates in .NET (in PowerShell, C# or VB)

datetime c#
c# datetime now
c# datetime format
new datetime c#
datetimeoffset
c# datetime parse
c# timespan
c# datetime today

What's the easiest way to perform an operation based on alternate dates?

Basically, I have 2 folders (dir1 & dir2) on Azure file storage, in which files need to be copied (over-written) on daily basis - one day in 'dir1', next day in 'dir2'.

I'm trying to write this in PowerShell (but happy with your answers in C# or VB).

I tried even/odd dates logic, but it breaks if month ends on 31st - because next date i.e. 1st is also an odd number (same problem with day-of-year as after 365 comes 1st both being odd numbers).

if((Get-Date).Day % 2 -eq 0 ) {"copy files in dir1"} else {"copy files in dir2"}

OR

if((Get-Date).DayOfYear % 2 -eq 0 ) {"copy files in dir1"} else {"copy files in dir2"}

I guess another method could be to read the "Last-Modified" file-attribute, but I'm struggling to read this as the containing folders reside on Azure cloud (in Azure File Storage).

I will appreciate any ideas. TIA


Instead of day of month or day of year, you could use day since some fixed time point:

int totalDays = (int)DateTime.UtcNow.Subtract(new DateTime(1970,1,1,0,0,0)).TotalDays;
if (totalDays % 2 == 0) { }
else { }

DateTime Struct (System), NET for more information and to determine whether your applications are An alternative to the DateTime structure for working with date and time values in constructor in C# and Visual Basic, as well as a DateTime declaration without  The .NET Framework and assemblies share the same version number. For example, .NET Framework versions include 4.5, 4.6.1, and 4.7.2. The common language runtime (CLR), which manages and executes your app's code. A single CLR version typically supports multiple .NET Framework versions.


You could just calculate the number of days since a fixed point in time and use that to determine even or odd.

if (((Get-Date) - [DateTime]'1970-01-01').Days % 2) {
    Write-Host 'odd day'
} else {
    Write-Host 'even day'
}

Choosing between DateTime, DateTimeOffset, TimeSpan, and , NET, typically where date and time information is stored in a simple data type. To uniquely and unambiguously identify a single point in time. 5 Install powershell module in closed network Jun 28 '18 4 Spell check should ignore underscore variable names in MS Word 2010 Feb 26 '17 3 Identify alternate dates in .NET (in PowerShell, C# or VB) Oct 17 '18


Here is the solution in PowerShell. I applied even/odd logic on the "number of days", which is calculated/generated by subtracting current-date with a fixed-date (as others suggested), I have written+tested it in PowerShell and can confirm it's working fine.

if(((Get-Date)-[DateTime]'2018-01-01').Days % 2) {'use dir1'} else{'use dir2'}

OR

$fixedDate = (Get-Date "01/01/2018")
$today = (Get-Date)
$ts = New-TimeSpan -Start $fixedDate -End $today
if(($ts).Days % 2 -eq 0) {"copy files in dir1"} else {"copy files in dir2"}

Thanks.

Use Culture Information in PowerShell to Format Dates, I have taught several Windows PowerShell, VBScript, and WMI classes in I can use the Get-Culture cmdlet to determine my current culture settings. In the DisplayAlternateShortDates.ps1 script, I create an array of assorted culture string names. NET Framework class to return culture names and IDs. It turns out that you can’t get the ADsType when binding to an attribute in the schema; instead, you have to retrieve the property from an actual object (such as a user account) and then you can determine the data type. That sounded way too complicated: you bind to the attribute in the schema to get one bit of information, then have to bind to an actual object to get the second bit of information.


Professional Microsoft Virtual Server 2005, Some alternative methods to consider are: ❑ You can simply change the main program to run with When PowerShell starts, it sets its own security impersonation level to identify. (This is necessary because, unlike in Visual Basic .NET or C#, PowerShell does not support importing calls to unmanaged libraries directly.)  The only correct way to test for formatted dates being greater or less than another is to format them in yyyyMMdd format. For example, using the below with myDatem being 05/07/2009 and today being 04/07/2010, you would get 05/07/2009 being later than today which is clearly not the case.


DateTime.TryParseExact Method (System), C#; VB; C++; F# TryParseExact(String, String, IFormatProvider, DateTimeStyles​, DateTime) NET for more information and to determine whether your applications are C# Copy. public static bool TryParseExact (ReadOnlySpan<​char> s,  However, I am running into a lot of issues with first loading the assembly, then using any of the types once the assembly is loaded. The complete file path is: C:\rnd\CloudBerry.Backup.API.dll. In Powershell I use: $dllpath = "C:\rnd\CloudBerry.Backup.API.dll" Add-Type -Path $dllpath. I get the error below:


Unblock-File, It unblocks PowerShell script files that were downloaded from the Internet so you can run Identifier alternate data stream, which has a value of "3" to indicate that it was This command shows how to find and unblock PowerShell scripts. type DateTime = struct interface IConvertible interface IFormattable interface ISerializable. type DateTime = struct interface IFormattable interface IConvertible. type DateTime = struct interface IFormattable interface IConvertible interface ISerializable. type DateTime = struct interface IFormattable.