search
HomeWeb Front-endJS TutorialIntroduction to javascript time zone function_time and date

js time zone function:

Suppose datename is a created Date object

================ ====
datename.getTimezoneOffset()
--Get the difference between local time and GMT time (Greenwich Mean Time), the return value is in minutes
======== ============

Example: Get GMT time and time in any time zone based on local time

d=new Date(); //Create a Date object
localTime = d.getTime();
localOffset=d.getTimezoneOffset()*60000; //Get the number of milliseconds of local time offset
utc = localTime localOffset; //utc is GMT time
offset =10; //Take Hawaii time as an example, East District 10
hawaii = utc (3600000*offset);
nd = new Date(hawaii);
document.writeln("Hawaii time is " nd.toLocaleString()
");

============================
Date.UTC(year, month, day, hours, minutes, seconds, ms)

--Construct a Date object based on GMT time and return a Date object
============ ===================

Creating a Date object directly will assume that the input is local time, while creating an object using Date.UTC will assume that the input is GMT. Time

============================
datename.toUTCString()
datename.toGMTString( )
--Output GMT time
================================

The two functions are the same .The latter is considered obsolete.

Relative to toLocaleString outputs local time, this function outputs GMT time

================== ===========
Others
==============================

getUTCDate() function--returns the day of the month (1-31) in the date object expressed in Universal Coordinated Time (UTC)
getUTCDay() function--returns the day of the month expressed in UTC in the date object The day of the week represented (0-6)
getUTCFulYear() function--returns the four-digit year in the date object expressed in Universal Coordinated Time (UTC)
getUTCHours() function--returns the four-digit year in the date object expressed in UTC The number of hours in Universal Coordinated Time (UTC) (0-23)
getUTCMilliseconds() function--returns the number of milliseconds in the date object expressed in Universal Coordinated Time (UTC) (0-999)
getUTCMinutes() Function -- Returns the number of minutes (0-59) expressed in Universal Coordinated Time (UTC) in the date object
getUTCMonth() Function -- Returns the number of months (0-59) expressed in Universal Coordinated Time (UTC) in the date object 11)
getUTCSeconds() function -- returns the number of seconds in the date object expressed in Universal Coordinated Time (UTC) (0-59)


setUTCDate() function -- sets the date object The day of the month expressed in Universal Coordinated Time (UTC), and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCFulYear() function -- sets the date object in world standard The year, month and day represented by time (UTC), and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCHours() function --- Set the world standard in the date object Hours, minutes, seconds and milliseconds represented by time (UTC), and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCMilliseconds() function--Set the world used in the date object The number of milliseconds expressed in standard time (UTC), and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCMinutes() function--Set the date object using Universal Standard Time (UTC) ) represents the minutes and seconds, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCMonth() function--Set the date object expressed in Universal Coordinated Time (UTC) Month, day, and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970
setUTCSeconds() function--Sets the seconds and milliseconds in the date object expressed in Universal Coordinated Time (UTC) , and returns the number of milliseconds (timestamp) between the date object and midnight on January 1, 1970

In the time zone in js, we generally talk about the time between Greenwich Time and local time The time difference is measured in minutes, which is no different from PHP and ASP. Let's use the date() function to introduce the related issues of js time zone.

Definition and Usage

The getTimezoneOffset() method returns the time difference between Greenwich Mean Time and local time, in minutes.
Syntax
dateObject.getTimezoneOffset() return value
The time difference between local time and GMT time, in minutes.
In the following example, we will get the time difference in minutes between GMT time and local time:

Copy code Code As follows:


Output:
-480
Let datename be a created Date object
================ ====
datename.getTimezoneOffset()
--Get the difference between local time and GMT time (Greenwich Mean Time), the return value is in minutes
======== ============

Example: Get GMT time and time in any time zone based on local time
Copy code The code is as follows:

d=new Date(); //날짜 객체 생성
localTime = d.getTime()
localOffset=d.getTimezoneOffset()*60000; 밀리초 수
utc = localTime localOffset; //utc는 GMT 시간입니다.
offset =10; //하와이 시간을 예로 들어 보겠습니다. 동부 10구역
hawaii = utc(3600000*offset); 🎜> nd = new Date(하와이);
document.writeln("하와이 시간은 " nd.toLocaleString()
");
Date.UTC(연, 월, 일, 시, 분 , 초, ms)
--GMT 시간을 기준으로 Date 객체를 구성하고 Date 객체를 반환합니다.

================== ====== =======
Date 객체를 직접 생성하면 입력이 현지 시간이라고 가정하고, Date.UTC를 사용하여 객체를 생성하면 입력이 GMT 시간이라고 가정합니다.
복사 코드는 다음과 같습니다
datename.toUTCString( )
datename.toGMTString()
--GMT 시간 출력
=================== =========
후자는 더 이상 사용되지 않는 것으로 간주됩니다.
toLocaleString과 관련하여 이 함수는

설명
getTimezoneOffset() 메소드는 현지 시간과 UTC 시간 사이의 분 수를 반환합니다. 기본적으로 이 함수는 JavaScript 코드가 실행되는 시간대와 지정된 시간이 일광 절약 시간제인지 여부를 알려줍니다. 🎜>반환이 시간이 아닌 분 단위로 이루어지는 이유. 일부 국가는 심지어 한 시간도 채 안 되는 시간대를 차지합니다.
팁 및 참고사항:
참고: 일광 절약 시간제 사용 관례로 인해 이 메소드의 반환 값은 상수가 아닙니다.
참고: The 메소드는 항상 Date 객체와 함께 사용됩니다.

var myDate=new Date()
Date 객체는 자동으로 저장됩니다.
매개변수 형식에는 다음과 같은 5가지 유형이 있습니다.


new Date("month dd,yyyy hh:mm :ss")
new Date("month dd,yyyy"); yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd) ;
new Date(ms)


마지막 형식에 유의하세요. 에서 매개변수는 생성되는 시간과 1970년 1월 1일 GMT 시간 사이의 밀리초 수를 나타냅니다. 다양한 함수의 의미는 다음과 같습니다.
month: 1월부터 12월까지 영어로 월 이름을 나타냅니다.
mth: (January)부터 11(December)까지, 월을 정수로 나타냅니다.
dd : 1부터 31까지의 일을 나타냅니다.
yyyy: 4자리 연도
hh: 시, 0(자정)부터 23(오후 11시)까지
mm: 분, 0의 정수 ~ 59
ss: 초 수, 0~59 사이의 정수
ms: 밀리초 수, 0보다 크거나 같은 정수
예:



코드 복사 코드는 다음과 같습니다. new Date("January 12,2006 22:19:35")
새 날짜("2006년 1월 12일");
새 날짜(2006,0,12,22,19,35)
새 날짜(2006,0,12); (1137075575000);


이전에는 js에도 시간대가 있다는 것을 몰랐습니다. 저는 항상 asp와 php에만 시간대가 있다고 생각했습니다. 도움이 필요한 학생들은 참고하시면 됩니다.
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools