Skip to content

Convert Days To Seconds

Updated: at 06:06 PM

Table of contents

Open Table of contents

Write a program to convert days to seconds

1 day = 24 hours = 1440 minutes = 86400 seconds

Input  : 5
Output : 432000

C

// Write a C program to convert days to seconds

#include<stdio.h> /* printf, scanf */

int ConvertToSeconds(int days);

int main()
{
    int days = 0;

    if(1 != scanf("%d",&days))
    {
        return -1;
    }

    printf("%d\n",ConvertToSeconds(days));

    return 0;
}

int ConvertToSeconds(int days)
{
    return days * 60 * 1440;
}

C#

// Write a C# program to convert days to seconds

public class ConvertDaysToSeconds
{
	private const int NoOfHoursInADay = 24;
    private const int NoOfMinutesForAnHour = 60;
    private const int NoOfSecondsForAMinute = 60;

    public static void Main()
    {
		Console.WriteLine("Please enter the no. of days to be converted to seconds");

        string userInput = Console.ReadLine();
        double value;

        if (double.TryParse(userInput, out value) && Convert.ToDouble(userInput) > 0)
        {
			Console.WriteLine($"Converted amount in seconds :  {CalculateSecondsForDays(Convert.ToDouble(userInput))}");
		}
        else
        {
			Console.WriteLine("INVALID INPUT !!!");
        }
    }

    private static double CalculateSecondsForDays(double noOfDays)
    {
		double totalHours = Convert.ToDouble(noOfDays) * NoOfHoursInADay;
        double totalMinutes = totalHours * NoOfMinutesForAnHour;
        return totalMinutes * NoOfSecondsForAMinute;
    }
}

C++

// Write a C++ program to convert days to seconds

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;cin>>n;
    cout<<(n*86400)<<'\n';
}

//Contributed by Raghav Garg

Dart

// Write a Dart program to convert days to seconds

int convertDaysToSeconds(int days) {
  int hours = 24, minutes = 60, seconds = 60, secondsInADay;
  secondsInADay = hours * minutes * seconds;
  return (days * secondsInADay).round();
}

F#

// Write a F# program to convert days to seconds

let daysToSeconds days : int =
  days * 86400

let seconds =  daysToSeconds 5

printfn "Equivalent seconds : %d" seconds

Go

// Write a Go program to convert days to seconds

package main

import "fmt"

func daysToSeconds(nums int) int {
	return nums * 86400
}

func main() {
	var i int
	fmt.Print("Input Days: ")
	fmt.Scan(&i)
	fmt.Println(daysToSeconds(i))
}

Haskell

-- Write a Haskell program to convert days to seconds

main :: IO ()
main = do
    putStrLn "Enter days: "
    input1 <- getLine
    let days = read input1 :: Float

    let seconds = days * 86400
    putStrLn $ show (seconds)

Java

// Write a Java program to convert days to seconds

import java.util.*;
import java.io.*;
public class Main
{
	public static void main(String[] args) {
	    Scanner sc=new Scanner(System.in);
	    int n = sc.nextInt();
	    int res=(n* 86400);
		System.out.println(res);
	 }
}

JavaScript

// Write a JavaScript program to convert days to seconds

function convertDaysToSeconds(days) {
  return days * 24 * 60 * 60;
}
console.log(convertDaysToSeconds(5));

Julia

# Write a Julia program to convert days to seconds

println("Enter value in days: ")
days = readline()
days = parse(Int, days)
seconds = days * 86400
println(days, " days equals ", seconds, " seconds")

Kotlin

// Write a Kotlin program to convert days to seconds

fun main() {
    print("Enter number of Days : ")
    println()
    var num_of_days = readLine()

    println("$num_of_days days = " + ConvertDaysToSeconds(num_of_days!!.toInt()) + " seconds")

}

private fun ConvertDaysToSeconds(days: Int): Int {
    val seconds = days * 24 * 60 * 60

    return  seconds
}

Perl

# Write a Perl program to convert days to seconds

#!/usr/bin/perl
use warnings;
use strict;
print "Enter the days : ";
my $days = <STDIN>;
my $seconds = $days * 86400;
print $days." days are ".$seconds." seconds\n";

PHP

// Write a PHP program to convert days to seconds

<?php
  $x = readline('Enter Days: ');

  $x1 = $x * 86400;
  echo "Seconds: $x1 \n";
?>

Python

# Write a Python program to convert days to seconds

a = int(input())

print(a * 86400)

# Contributed by Raghav Garg

R

# Write a R program to convert days to seconds

convertDaysToSeconds = function(days) {
  return(days * 24 * 60 * 60)
}

print(convertDaysToSeconds(5))
#output: 432000

Ruby

# Write a Ruby program to convert days to seconds

def convert_days_to_seconds(days = 5)
  hours = 24
  minutes = 60
  seconds = 60
  seconds_in_day = hours * minutes * seconds
  return (seconds_in_day * days).round()
end

Rust

// Write a Rust program to convert days to seconds

use std::io;

fn main() {
    println!("Enter the number of days:");
    let mut input = String::new();
    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read input.");

    let days: u32 = match input.trim().parse() {
        Ok(num) => num,
        Err(_) => {
            println!("Invalid input. Please enter a valid number of days.");
            return;
        }
    };

    let seconds = days * 86400;

    println!("{} days is equal to {} seconds.", days, seconds);
}

Scala

// Write a Scala program to convert days to seconds

object HelloWorld {
	def main(args: Array[String]): Unit = {
	  val days = scala.io.StdIn.readInt();
	  println("Seconds: " + days * 86400);
	}
}

Swift

// Write a Swift program to convert days to seconds

import Swift
import Foundation

var days = 5

var seconds = days * 86400
print("Seconds: ", seconds)

TypeScript

// Write a TypeScript program to convert days to seconds

function convertDaysToSeconds(days: number): number {
  let hours = 24,
    minutes = 60,
    seconds = 60,
    secondsInADay;
  secondsInADay = hours * minutes * seconds;
  return Math.round(days * secondsInADay);
}

console.log(convertDaysToSeconds(5));

Similar programs