Rounding minutes to nearest 5 mins interval

I’m getting time outputs which I want to turn into rounded numbers. Example: If I get 10:31 AM, I want to turn it into 10:30 AM. If it’s 10:34 AM it should be rounded to 10:35 AM.

How can I do that?

Hello @NunuNana,
My first thought was to get minutes from the time by using either a split() function or formatDate() function.
Then, parseNumber() to turn that result into a number.
Perform a mod 5 on that number.
If the mod result is 0, do nothing.
For result 1, subtract 1 from minutes and use setMinute() to set the time on the original date/time.
For result 2, subtract 2.
For result 3, add 2.
For result 4, add 1.

For 10:31, that’s 31 mod 5 = 1.
Subtract 1 minute from :31 to get 10:30.

For 10:34, that’s 34 mod 5 = 4.
Add 1 minute to :34 to get 10:35.

For something like 10:59, 59 mod 5 = 4.
Add 1 minute to :59 to get 10:60 which Make should result in 11:00.

I’m just wondering what better ways there are to get this done.

1 Like

Welcome to the Make community!

Yes, that is possible. You’ll need a minimum of one module:

{{formatDate(setMinute(parseDate(92.time; "HH:mm A"); round(formatDate(parseDate(92.time; "HH:mm A"); "mm") * 0.2) * 5); "HH:mm A")}}

Examples

Input

Screenshot_2024-07-29_110717

Output

Give it a go and let us know if you have any issues!

Explanation (sub-steps)

Hope this helps!

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

1 Like

Module Export

You can copy and paste this module export into your scenario. This will paste the modules shown in my screenshots above.

  1. Copy the JSON code below by clicking the copy button when you mouseover the top-right of the code block
    Screenshot_2024-01-17_200117

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the canvas.

  3. Click on each imported module and save it for validation. You may be prompted to remap some variables and connections.

Click to Expand Module Export Code

JSON - Copy and Paste this directly in the scenario editor

{
    "subflows": [
        {
            "flow": [
                {
                    "id": 92,
                    "module": "json:ParseJSON",
                    "version": 1,
                    "parameters": {
                        "type": ""
                    },
                    "mapper": {
                        "json": "[\n{ \"time\": \"11:55 AM\" },\n{ \"time\": \"11:56 AM\" },\n{ \"time\": \"11:57 AM\" },\n{ \"time\": \"11:58 AM\" },\n{ \"time\": \"11:59 AM\" },\n{ \"time\": \"12:00 PM\" }\n]"
                    },
                    "metadata": {
                        "designer": {
                            "x": -3,
                            "y": -2877
                        },
                        "restore": {
                            "parameters": {
                                "type": {
                                    "label": "Choose a data structure"
                                }
                            }
                        },
                        "parameters": [
                            {
                                "name": "type",
                                "type": "udt",
                                "label": "Data structure"
                            }
                        ],
                        "expect": [
                            {
                                "name": "json",
                                "type": "text",
                                "label": "JSON string",
                                "required": true
                            }
                        ]
                    }
                },
                {
                    "id": 94,
                    "module": "util:SetVariables",
                    "version": 1,
                    "parameters": {},
                    "mapper": {
                        "variables": [
                            {
                                "name": "parsed_date",
                                "value": "{{parseDate(92.time; \"HH:mm A\")}}"
                            },
                            {
                                "name": "rounded_minutes",
                                "value": "{{round(formatDate(parseDate(92.time; \"HH:mm A\"); \"mm\") * 0.2) * 5}}"
                            },
                            {
                                "name": "set_minutes",
                                "value": "{{setMinute(parseDate(92.time; \"HH:mm A\"); round(formatDate(parseDate(92.time; \"HH:mm A\"); \"mm\") * 0.2) * 5)}}"
                            },
                            {
                                "name": "output_time",
                                "value": "{{formatDate(setMinute(parseDate(92.time; \"HH:mm A\"); round(formatDate(parseDate(92.time; \"HH:mm A\"); \"mm\") * 0.2) * 5); \"HH:mm A\")}}"
                            }
                        ],
                        "scope": "roundtrip"
                    },
                    "metadata": {
                        "designer": {
                            "x": 244,
                            "y": -2876
                        },
                        "restore": {
                            "expect": {
                                "variables": {
                                    "items": [
                                        null,
                                        null,
                                        null,
                                        null
                                    ]
                                },
                                "scope": {
                                    "label": "One cycle"
                                }
                            }
                        },
                        "expect": [
                            {
                                "name": "variables",
                                "type": "array",
                                "label": "Variables",
                                "spec": [
                                    {
                                        "name": "name",
                                        "label": "Variable name",
                                        "type": "text",
                                        "required": true
                                    },
                                    {
                                        "name": "value",
                                        "label": "Variable value",
                                        "type": "any"
                                    }
                                ]
                            },
                            {
                                "name": "scope",
                                "type": "select",
                                "label": "Variable lifetime",
                                "required": true,
                                "validate": {
                                    "enum": [
                                        "roundtrip",
                                        "execution"
                                    ]
                                }
                            }
                        ],
                        "interface": [
                            {
                                "name": "parsed_date",
                                "label": "parsed_date",
                                "type": "any"
                            },
                            {
                                "name": "rounded_minutes",
                                "label": "rounded_minutes",
                                "type": "any"
                            },
                            {
                                "name": "set_minutes",
                                "label": "set_minutes",
                                "type": "any"
                            },
                            {
                                "name": "output_time",
                                "label": "output_time",
                                "type": "any"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "metadata": {
        "version": 1
    }
}

samliewrequest private consultation

Join the Make Fans Discord server to chat with other makers!

This is a slick solution!

Screenshot 2024-07-29 at 13.27.12
@samliew @Donald_Mitchell Thank you all!
This is how I solved it!

2 Likes