تبدیل String به Date در جاوا اسکریپت – آموزش ۳ حالت رایج

تبدیل String به Date در جاوا اسکریپت

امروز به شما آموزش تبدیل String به Date در جاوا اسکریپت را آموزش می دهیم.

تبدیل String به Date در جاوا اسکریپت

در ۳ مثال زیر می توانید حالت های مختلف تبدیل String به Date در جاوا اسکریپت را مشاهده نمایید.

 

مثال ۱ :

 

<!DOCTYPE html> 
<html> 
<html dir="rtl" lang="ar">
<head> 
	<title>Page Title</title> 
</head> 

<body> 
	<center> 
		<h1 style="color:green">Farazgar</h1> 
		<p>تبدیل String به Date در جاوا اسکریپت</p> 
		<script> 
			//It returns the Day,Month,Date,Year and time 
			var d = new Date("May 1,2019 11:20:00"); 
			document.write(d); 
		</script> 
	</center> 
</body> 

</html>

 

نتیجه :

تبدیل String به Date در جاوا اسکریپت

 

مثال ۲ :

 

<!DOCTYPE html> 
<html> 
<html dir="rtl" lang="ar">
<head> 
	<title>Page Title</title> 
</head> 

<body> 
	<center> 
		<h1 style="color:green"> 
	Farazgar</h1> 
		<p>تبدیل String به Date در جاوا اسکریپت</p> 
		<script> 
			var d = new Date("May 1, 2019 "); 
			document.write(formatDate(d)); 

			function formatDate(date) { 
				var day = date.getDate(); 
				if (day < 10) { 
					day = "0" + day; 
				} 
				var month = date.getMonth() + 1; 
				if (month < 10) { 
					month = "0" + month; 
				} 
				var year = date.getFullYear(); 
				return day + "/" + month + "/" + year; 
			} 
		</script> 
	</center> 
</body> 

</html>

 

نتیجه :

تبدیل String به Date در جاوا اسکریپت

 

مثال ۳ :

 

<!DOCTYPE html> 
<html> 
<html dir="rtl" lang="ar">
<head> 
	<title>Page Title</title> 
</head> 

<body> 
	<center> 
		<h1 style="color:green"> 
		Farazgar</h1> 
		<p> 
		تبدیل String به Date در جاوا اسکریپت
	</p> 
		<script> 
			var date = new Date(2019, 5, 3); 

			document.write(date.toDateString()); 
		</script> 
	</center> 
</body> 

</html> 

 

نتیجه :

 

از اینکه فرازگر را در این مقاله همرایی کردید از شما سپاسگذاریم.

5/5 - (1 امتیاز)

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *