Skip to content

Commit

Permalink
feat(docs): add mdx file for secondsToTime function
Browse files Browse the repository at this point in the history
  • Loading branch information
xGooddevilx authored and ASafaeirad committed Nov 4, 2024
1 parent 271d112 commit 25bd6b8
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions docs/pages/function/seconds-to-time.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# secondsToTime

Formats a number of seconds into a string representation based on a specified format.


### Import

```typescript copy
import { secondsToTime } from '@fullstacksjs/toolbox';
```

### Signature

```typescript copy
function secondsToTime({
sec,
format,
}: {
sec: number;
format: 'hh:mm:ss' | 'hh:mm' | 'mm:ss';
}): string;
```

### Examples

```typescript copy
sleep(1000).then(() => console.log("Hi")) // Logs hi after 1 second
await sleep(1000); console.log("Hi") // Logs hi after 1 second
```


## Import

```typescript copy
import { secondsToTime } from './secondsToTime';
```

## Signature

```typescript
function secondsToTime({
sec,
format,
}: {
sec: number;
format: 'hh:mm:ss' | 'hh:mm' | 'mm:ss';
}): string;
```

## Parameters

- **`sec`** (`number`): The total number of seconds to be converted.
- **`format`** (`'hh:mm:ss' | 'hh:mm' | 'mm:ss'`): The desired output format.

## Returns

- **`string`**: The formatted time string based on the specified format.

## Examples

### Formatting to `hh:mm:ss`

```typescript
const time1 = secondsToTime({ sec: 3661, format: 'hh:mm:ss' });
// Output: '01:01:01'
```

### Formatting Zero Seconds

```typescript
const time4 = secondsToTime({ sec: 0, format: 'mm:ss' });
// Output: '00:00'
```

## Error Handling

If an invalid format is provided, the function will throw an error:

```typescript
try {
secondsToTime({ sec: 120, format: 'ss:ss:ss' });
} catch (e) {
console.error(e.message); // 'Invalid format: 'ss:ss:ss'. Accepted formats are 'hh:mm:ss', 'hh:mm', or 'mm:ss'.'
}
```

0 comments on commit 25bd6b8

Please sign in to comment.