Skip to main content

Notifications

Announcements

No record found.

Service | Customer Service, Contact Center, Fie...
Suggested answer

Convert date/time field to date only

(0) ShareShare
ReportReport
Posted on by 160

Hi.

I have a field was in date/time format. i change it to date only format and set to date only for field behavior also. Now, i want to how can i change the old records that are saved in date/time in database to "date only" by C# code.

kind regards,

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Convert date/time field to date only

    Hi,

    Do you change the DateTime attribute’s Behaviour from User Local to TimezoneIndependent ? If you do, the datetime property value will not be converted if you execute the ConvertDateAndTimeBehavior request.

    If this helped you, I'd appreciate it if you'd mark this as a Verified Answer, which may in turn help others as well.

    Best Regards,

    Frank Gong

  • Suggested answer
    CRMJetty Profile Picture
    CRMJetty 3,508 on at
    RE: Convert date/time field to date only

    Hello,

    Try this one,

    https://www.inogic.com/blog/2015/07/updating-date-values-of-existing-records-after-modifying-the-datetime-behaviour-in-dynamics-crm-online-update-1/

    https://forums.asp.net/t/2003945.aspx?Convert+DateTime+in+C+into+Date+in+SQL

    I hope it helps,

    Thanks.

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365

  • Nafas Profile Picture
    Nafas 160 on at
    RE: Convert date/time field to date only

    Hi. I write this code but when it runs successfully, the data does not change anymore.

    var request = new ConvertDateAndTimeBehaviorRequest()
    {
    Attributes = new EntityAttributeCollection() { new KeyValuePair<string, StringCollection>("pcm_contract", new StringCollection() {"pcm_startdate"}) },
    ConversionRule = DateTimeBehaviorConversionRule.SpecificTimeZone.Value,
    TimeZoneCode = 110,
    AutoConvert = false // Conversion must be done using ConversionRule
    };

    // Execute the request
    var response = (ConvertDateAndTimeBehaviorResponse)service.Execute(request);

    }

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Convert date/time field to date only

    Hi,

    You can use CrmServiceClient to get and update records in CRM. Here are some code samples:

    using(var svc = new CrmServiceClient(connectionstring))
       {
                CreateRequiredRecords();
    
         // Instantiate an account object.
         // See the Entity Metadata topic in the SDK documentation to determine
         // which attributes must be set for each entity.
    
               var account = new Account { Name = "Fourth Coffee" };
    
         // Create an account record named Fourth Coffee.
               _accountId = svc.Create(account);
               Console.Write("{0} {1} created, ", account.LogicalName, account.Name);
    
         // Retrieve the account containing several of its attributes.
               var cols = new ColumnSet(
               new String[] { "name", "address1_postalcode", "lastusedincampaign", "versionnumber" });
               Account retrievedAccount = (Account)svc.Retrieve("account", _accountId, cols);
               Console.Write("retrieved ");
    
         // Retrieve version number of the account. Shows BigInt attribute usage.
               long? versionNumber = retrievedAccount.VersionNumber;
               if (versionNumber != null)
               Console.WriteLine("version # {0}, ", versionNumber);
    
         // Update the postal code attribute.
               retrievedAccount.Address1_PostalCode = "98052";
    
         // The address 2 postal code was set accidentally, so set it to null.
               retrievedAccount.Address2_PostalCode = null;
    
          // Shows usage of option set (picklist) enumerations defined in OptionSets.cs.
               retrievedAccount.Address1_AddressTypeCode = new OptionSetValue((int)AccountAddress1_AddressTypeCode.Primary);
               retrievedAccount.Address1_ShippingMethodCode = new OptionSetValue((int)AccountAddress1_ShippingMethodCode.DHL);
               retrievedAccount.IndustryCode = new OptionSetValue((int)AccountIndustryCode.AgricultureandNonpetrolNaturalResourceExtraction);
    
          // Shows use of a Money value.
               retrievedAccount.Revenue = new Money(5000000);
    
          // Shows use of a Boolean value.
               retrievedAccount.CreditOnHold = false;
    
          // Shows use of EntityReference.
               retrievedAccount.ParentAccountId = new EntityReference(Account.EntityLogicalName, _parentAccountId);
    
               // Shows use of Memo attribute.
               retrievedAccount.Description = "Account for Fourth Coffee.";
    
               // Update the account record.
               svc.Update(retrievedAccount);
               Console.WriteLine("and updated.");
               DeleteRequiredRecords(promptforDelete);
               }

    Then you can use DateTime.ParseExact to convert the obtained date into a date-only format, and then update the field:

    public static DateTime ParseExact (string s, string format, IFormatProvider? provider);

    This is the code sample:

    using System;
    					
    public class Program
    {
    	public static void Main()
    	{
    		DateTime date = DateTime.ParseExact("9/1/2009", "M/d/yyyy", System.Globalization.CultureInfo.InvariantCulture);
    		Console.WriteLine(date.ToString("yyyyMMdd"));
    	}
    }

    If this helped you, I'd appreciate it if you'd mark this as a Verified Answer, which may in turn help others as well.

    Best Regards,

    Frank Gong

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,431 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans