Working with dates in javascript

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

To create date object in JavaScript use Date() constructor

The following example writes the current Date and Time to the web page

If the Date() constructor is used without any arguments, it returns the current date and time. To create a date object with specific dates there are 2 ways.

Creating a specific date object in JavaScript using a date string
var dateOfBirth = new Date("January 13, 1980 11:20:00");

You can also create a specific date object using number for year, month, day, hours, minutes, seconds, & milliseconds. The syntax is shown below.
var dateOfBirth = new Date(year, month, day, hours, minutes, seconds, milliseconds);

Example :
var dateOfBirth = new Date(1980, 0, 13, 11, 20, 0, 0);

Please note : In JavaScript month numbers start from ZERO. So if you want the month of march then use 2 instead of 3.

The above code produces the following output on my machine, because I have (UTC) Dublin, Edinburgh, Lisbon, London time zone selected on my machine
Sun Jan 13 1980 11:20:00 GMT+0000 (GMT Standard Time)

If you have a different time zone selected on your computer, you may get a slightly different result. For example if you have (UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi time zone selected, the result will be as shown below
Sun Jan 13 1980 11:20:00 GMT+0530 (India Standard Time)

Some useful Date object methods in JavaScript
getFullYear() - Returns the full year (all the four digits)

Example : The following example returns 1980
var year = new Date(1980, 0, 13, 11, 20, 0, 0).getFullYear();

getMonth() - Returns the month number (from 0-11)

Example : The following example returns 0 (for January)
var month = new Date(1980, 0, 13, 11, 20, 0, 0).getMonth();

You can use the following code to get the month name from the month number in Javascript. The following example returns January.
function getMonthNameFromNumber(monthNumber)
{
var monthNames = ["January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"];
return monthNames[monthNumber];
}

var monthName = getMonthNameFromNumber(new Date(1980, 0, 13, 11, 20, 0, 0).getMonth());

getDate() - Returns the day of the month (from 1-31)

Example : The following example returns 13
var dayOfMonth = new Date(1980, 0, 13, 11, 20, 0, 0).getDate();

getDay() - Returns the day number of the week (from 0-6). 0 is sunday, 1 is monday and so on.

Example : The following example returns 0
var dayOfWeek = new Date(1980, 0, 13, 11, 20, 0, 0).getDay();

You can use the following code to get the day of the week from the day number in Javascript. The following example returns Sunday.
function getWeekDayNameFromNumber(dayNumber)
{
var weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
return weekDays[dayNumber];
}

var weekdayName = getWeekDayNameFromNumber(new Date(1980, 0, 13, 11, 20, 0, 0).getDay());

You also have the following methods that return the time parts of the date object
getHours() - Returns the hour (from 0-23)
getMinutes() - Returns the minutes (from 0-59)
getSeconds() - Returns the seconds (from 0-59)
getMilliseconds() - Returns the milliseconds (from 0-999)

How to convert date in javascript to dd/mm/yyyy format
function formatDate(date)
{
if (day [ 10)
{
day = "0" + day;
}

if (month [ 10)
{
month = "0" + month;
}

return day + "/" + month + "/" + year;
}

If you don't want ZERO (0) before a single digit month or day number, then modify the formatDate() function as shown below.
function formatDate(date)
{

return day + "/" + month + "/" + year;
}

Рекомендации по теме
Комментарии
Автор

Than you. This video tutorial is really nice. I enjoyed it because it's simple and well - explained. The Blog and code sample was very helpful

AMINULISLAM-zxmg
Автор

Excellent training, you are a natural teacher, Bravo!

edessamediagroup
Автор

Thank you for this tutorial, but I must say you sound like Indian Arnold Swarzeneger :D

_mitric_n
Автор

I love your tutorial. It helped me a lot. Thanks

sadie
Автор

this is a real tutorial. the other solutions point to jquery that is just copy and paste.

antidotejack
Автор

This is completely clear! Thank you very much!

АльбертоСигары
Автор

great channel i learn javascript quite much

manishchaudhary
Автор

thankx for this tutorial....you have explained it clearly..everything about date(); constructor

swapnilsonsurkar
Автор

thank you this video helps me a lot thanks again

atulcodex
Автор

+kudvenkat :: You are awesome! Hats off!!

ImroseRezaHimel
Автор

In a scenario like you are having an options of 1-24 in a drop-down list, and whatever value you are going to select (1-24) it'll show you the date after that month. i.e- if you select 2, it'll show you the date of 2 month's later date. How to implement this?

PritamBiswasp_cristiano
Автор

Hi Can you tell me how to write a script so that when you select a date it will auto populate another field with a specific rate for example if the date selected is a weekday it will populate another field with £65 and if date selected is a weekend it will populate the other field with £130?

ciaranmaguire
Автор

I was trying to find more javascript tutorials but was not able to. You explained your youtube channel lay out but I could not find the search icon to search through your videos. finally, I don't know if your voice volume is the same on all your videos, but the working with dates volume is low, I have my computer volume all the way up but still somewhat faint. Looking forward to your reply.

edessamediagroup
Автор

hi sir...i want to display current date (yyyy-mm-dd) to my project..as i am doing a small project like billling..how to display current date without user interaction.

azeemarab
Автор

Hi Venkat, What text editor you used for this video? Thanks.

taylorlee
Автор

can anyone help with this question ?

With javascript or php or java, using date objects write a function which calculates the number of days since your birth, this number of days should be displayed on a web page ( the birth date may be hard coded )?

husumi
Автор

Thanks Venkat! When will part 33 (and after) be uploaded onto the shared folders? Ty.

wgh
Автор

Please make tutorial for Php framework like laravel

manishchaudhary
Автор

hi sir how to avoid previous date using calendar in javascript

asharudeena
Автор

i dont know why 4 ppl disliked this video

swapnilsonsurkar