How to add more date time format in Spread JS

Posted by: gagandeep.singh on 8 June 2023, 10:43 am EST

  • Posted 8 June 2023, 10:43 am EST - Updated 8 June 2023, 10:48 am EST

    Right now, when we try to format date. Then it shows only 16 formats by default. Can we add more like 20 more? I want to add it in the Date not in the custom

  • Posted 9 June 2023, 4:02 pm EST

    Hi Gagandeep,

    You need to add the new date/time formats to the Predefined Formats of the culture. Kindly refer to the following code snippet and the sample. You could also refer to the below Docs References:

    
    let newDateFormats = ["MMMM-yy", "d-MMM-yyyy", "MMMM d, yyyy", "M/d/yy HH:mm tt", "M/d/yy H:mm"];
    let newTimeFormats = ["HH:mm:ss tt", "HH:mm", "HH:mm tt", "HH:mm:ss", "mm:ss.0"];
    
    // Get the culture info instance
    let culture = new GC.Spread.Common.CultureInfo();
    let en_culture = GC.Spread.Common.CultureManager.getCultureInfo('en-us');
    culture.predefinedFormats = Object.assign({}, en_culture.predefinedFormats);
    
    // Add new Date formats
    culture.predefinedFormats.Date = culture.predefinedFormats.Date.concat(newDateFormats);
    
    // Add new Time formats
    culture.predefinedFormats.Time = culture.predefinedFormats.Time.concat(newTimeFormats);
    
    // get current culture name
    let cultureName = GC.Spread.Common.CultureManager.culture();
    
    //set infos for this culture
    GC.Spread.Common.CultureManager.addCultureInfo(cultureName, culture);
    

    Sample: https://jscodemine.grapecity.com/share/dxd4Xk99PkGuq09c7FHa0A/?IsEmbed=false&Theme=Unset&PreviewDirection=0&IsEditorShow=true&IsExplorerShow=true&IsPreviewShow=true&IsConsoleShow=true&IsRunBTNShow=false&IsResetBTNShow=false&IsOpenInCodemineBTNShow=false&PanelWidth=20&PanelWidth=50&PanelWidth=30&defaultOpen={"OpenedFileName"%3A["%2Findex.html"%2C"%2Fpackage.json"%2C"%2Fsrc%2Fapp.js"]%2C"ActiveFile"%3A"%2Fsrc%2Fapp.js"}

    References:

    Customize Format Culture Dialog: https://www.grapecity.com/spreadjs/docs/spreadjs_designer_component/customizations/designer-customize-culture

    CultureManager Class:

    https://www.grapecity.com/spreadjs/api/v16/classes/GC.Spread.Common.CultureManager#class-culturemanager

    Please let us know if you face any problems.

    Regards,

    Ankit

  • Posted 12 June 2023, 9:32 am EST

    Thank you so much Ankit

  • Posted 12 June 2023, 2:55 pm EST

    Hi @Ankit,

    Your code throws me error at concat because there is no property date exist in predefinedFormats. I am trying to do something like this:

    var designer = new GC.Spread.Sheets.Designer.Designer(document.getElementById("gc-designer-container"), config);
    var activeSheet = designer.Spread.getActiveSheet();
    
    let newDateFormats = ["MMMM-yy", "d-MMM-yyyy", "MMMM d, yyyy", "M/d/yy HH:mm tt", "M/d/yy H:mm", "M/d/yy -- MMM/d--yyyy H:mm"];
    
    
    let culture = new GC.Spread.Common.CultureInfo();
    let en_culture = GC.Spread.Common.CultureManager.getCultureInfo('en-us');
    culture.predefinedFormats = Object.assign({}, en_culture.predefinedFormats);
    
    			// Add new Date formats
    			culture.predefinedFormats.Date = newDateFormats;
    
    			// Add new Time formats
    			culture.predefinedFormats.Time = newTimeFormats;
    
    			// get current culture name
    			let cultureName = GC.Spread.Common.CultureManager.culture();
    
    			//set infos for this culture
    			GC.Spread.Common.CultureManager.addCultureInfo(cultureName, culture);

    You see I am trying to add this format “M/d/yy – MMM/d–yyyy H:mm” but it doesn’t reflect in date section of format cells. Only predefined/default 16 formats are showing. Thanks

  • Posted 13 June 2023, 2:42 am EST - Updated 13 June 2023, 2:47 am EST

    Hi Gagandeep,

    Could you kindly share us a minimal working sample replicating the issue? You may refer to the above sample that I have shared and it was successfully adding the new formats to the Time and Date Tabs.

    Please note that this feature is available in the SpreadJS V15.1.0 and later version. Make sure you are using the SpreadJS V15.1.0 or later.

    Regards,

    Ankit

  • Posted 13 June 2023, 10:22 am EST - Updated 13 June 2023, 10:27 am EST

    Thanks Ankit.

    I am using 15.0.0 version that’s why. Also when I am trying to upgrade to latest version for this functionality it throws me invalid license key error.

    Do I need to generate difference license key for this version?

  • Posted 13 June 2023, 11:38 pm EST

    Hi Gagandeep,

    As mentioned, this feature is currently supported in SpreadJS V15.1.0 and later versions, and since you are using the V15.0.0, it may not work. You may upgrade to the higher version of SpreadJS. For example, the last release of SpreadJS V15 was V15.2.5. You may upgrade to the SpreadJS V15.2.5, and it would work fine.

    For SpreadJS, the same license key works for the minor updates, for example, if you have the License Key for the SpreadJS V15.0.0, it will work for all the minor updates like V15.x.x.

    For major updates like V16, you would be required a new license key. For license related queries, it is better that you create a new ticket on our private portal at: https://www.grapecity.com/my-account/my-support.

    Also, kindly share the information like the hostnames, license key details when creating a case on private portal.

    Please let me know if you still face any issues. We would be more than happy to help you.

    Regards,

    Ankit

  • Posted 14 June 2023, 11:08 am EST

    Perfect thanks all good sir

  • Posted 14 June 2023, 12:50 pm EST

    Hi Ankit,

    Now I can add all formatting, I have small question if I want to add formatting for capital letters like:

    DECEMBER 31, 2041

    What formatting I should add?

  • Posted 15 June 2023, 2:58 am EST - Updated 15 June 2023, 3:03 am EST

    Hi,

    We are sorry but This will not be achieved using formatters you may need to use the Upper and Text functions. Please refer to the following formula and image that explains the same.

    `=UPPER(TEXT(E6,“mmmm dd, yyyy”))

    Further, if want to achieve it using formatter you may need to create a custom formatter. Please refer to the following demos that explain how you could create your own formatter in spreadJS.

    Also note that, If you implement customer formatted then the formatter now works after exporting it in Excel since spreadJS customer format is not supported by Excel

    custom format: https://www.grapecity.com/spreadjs/demos/features/cells/formatter/custom-formatter#demo_source_name

    regards,

    Avinash

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels