[{"data":1,"prerenderedAt":314},["ShallowReactive",2],{"page:/guides/get-current-timestamp-in-code":3},{"id":4,"title":5,"body":6,"description":301,"extension":302,"eyebrow":303,"faq":304,"locale":305,"meta":306,"navigation":215,"pageType":307,"path":308,"related":304,"seo":309,"stem":312,"__hash__":313},"pages/en/guides/get-current-timestamp-in-code.md","Get the current Unix timestamp in code",{"type":7,"value":8,"toc":291},"minimark",[9,14,79,83,105,109,125,129,137,141,145,164,185,189,235,241,245,256,264,268,287],[10,11,13],"h2",{"id":12},"javascript","JavaScript",[15,16,21],"pre",{"className":17,"code":18,"language":19,"meta":20,"style":20},"language-js shiki shiki-themes github-light github-dark","const milliseconds = Date.now()\nconst seconds = Math.floor(milliseconds / 1000)\n","js","",[22,23,24,51],"code",{"__ignoreMap":20},[25,26,29,33,37,40,44,48],"span",{"class":27,"line":28},"line",1,[25,30,32],{"class":31},"szBVR","const",[25,34,36],{"class":35},"sj4cs"," milliseconds",[25,38,39],{"class":31}," =",[25,41,43],{"class":42},"sVt8B"," Date.",[25,45,47],{"class":46},"sScJk","now",[25,49,50],{"class":42},"()\n",[25,52,54,56,59,61,64,67,70,73,76],{"class":27,"line":53},2,[25,55,32],{"class":31},[25,57,58],{"class":35}," seconds",[25,60,39],{"class":31},[25,62,63],{"class":42}," Math.",[25,65,66],{"class":46},"floor",[25,68,69],{"class":42},"(milliseconds ",[25,71,72],{"class":31},"/",[25,74,75],{"class":35}," 1000",[25,77,78],{"class":42},")\n",[10,80,82],{"id":81},"python","Python",[15,84,87],{"className":85,"code":86,"language":81,"meta":20,"style":20},"language-python shiki shiki-themes github-light github-dark","import time\nseconds = int(time.time())\nmilliseconds = time.time_ns() // 1_000_000\n",[22,88,89,94,99],{"__ignoreMap":20},[25,90,91],{"class":27,"line":28},[25,92,93],{},"import time\n",[25,95,96],{"class":27,"line":53},[25,97,98],{},"seconds = int(time.time())\n",[25,100,102],{"class":27,"line":101},3,[25,103,104],{},"milliseconds = time.time_ns() // 1_000_000\n",[10,106,108],{"id":107},"java","Java",[15,110,113],{"className":111,"code":112,"language":107,"meta":20,"style":20},"language-java shiki shiki-themes github-light github-dark","long milliseconds = System.currentTimeMillis();\nlong seconds = java.time.Instant.now().getEpochSecond();\n",[22,114,115,120],{"__ignoreMap":20},[25,116,117],{"class":27,"line":28},[25,118,119],{},"long milliseconds = System.currentTimeMillis();\n",[25,121,122],{"class":27,"line":53},[25,123,124],{},"long seconds = java.time.Instant.now().getEpochSecond();\n",[10,126,128],{"id":127},"php-go-sql-and-shell","PHP, Go, SQL and Shell",[15,130,135],{"className":131,"code":133,"language":134,"meta":20},[132],"language-text","PHP:   time()\nGo:    time.Now().Unix()\nPostgreSQL: floor(extract(epoch from now()))\nShell: date +%s\n","text",[22,136,133],{"__ignoreMap":20},[138,139,140],"p",{},"Check whether the receiving system expects seconds or milliseconds. The unit mismatch is more common than a wrong clock.",[10,142,144],{"id":143},"verify-the-unit-before-using-a-snippet","Verify the unit before using a snippet",[138,146,147,148,151,152,155,156,159,160,163],{},"JavaScript ",[22,149,150],{},"Date.now()"," returns milliseconds. Python ",[22,153,154],{},"time.time()"," returns seconds with a fractional part; ",[22,157,158],{},"time.time_ns()"," returns integer nanoseconds. Java's ",[22,161,162],{},"Instant"," separates whole epoch seconds from a nanosecond adjustment. Do not append zeros to a lower-resolution reading and describe it as newly measured precision.",[138,165,166,167,170,171,174,175,178,179,184],{},"Use the fixed fixture ",[22,168,169],{},"1700000000"," seconds, or ",[22,172,173],{},"1700000000000"," milliseconds, in a unit test. Both should produce ",[22,176,177],{},"2023-11-14T22:13:20Z"," in the ",[180,181,183],"a",{"href":182},"/timestamp-to-date/","timestamp converter",". Fixed fixtures make an off-by-1,000 error easier to identify than a changing current-time value.",[10,186,188],{"id":187},"javascript-date-and-monotonic-time","JavaScript date and monotonic time",[15,190,193],{"className":191,"code":192,"language":12,"meta":20,"style":20},"language-javascript shiki shiki-themes github-light github-dark","const milliseconds = Date.now()\nconst seconds = Math.floor(milliseconds / 1000)\nconst isoUtc = new Date(milliseconds).toISOString()\n\nconst started = performance.now()\n// Run the operation you want to measure.\nconst elapsedMilliseconds = performance.now() - started\n",[22,194,195,200,205,210,217,223,229],{"__ignoreMap":20},[25,196,197],{"class":27,"line":28},[25,198,199],{},"const milliseconds = Date.now()\n",[25,201,202],{"class":27,"line":53},[25,203,204],{},"const seconds = Math.floor(milliseconds / 1000)\n",[25,206,207],{"class":27,"line":101},[25,208,209],{},"const isoUtc = new Date(milliseconds).toISOString()\n",[25,211,213],{"class":27,"line":212},4,[25,214,216],{"emptyLinePlaceholder":215},true,"\n",[25,218,220],{"class":27,"line":219},5,[25,221,222],{},"const started = performance.now()\n",[25,224,226],{"class":27,"line":225},6,[25,227,228],{},"// Run the operation you want to measure.\n",[25,230,232],{"class":27,"line":231},7,[25,233,234],{},"const elapsedMilliseconds = performance.now() - started\n",[138,236,237,238,240],{},"The first three expressions refer to calendar time. The last two measure a duration with a monotonic clock and are not Unix timestamps. A system clock correction can change ",[22,239,150],{},", whereas an elapsed-duration timer has a different purpose.",[10,242,244],{"id":243},"transport-large-integers-safely","Transport large integers safely",[138,246,247,248,251,252,255],{},"A 19-digit nanosecond value should not be converted to a JavaScript ",[22,249,250],{},"Number"," before parsing. Carry it as a decimal string in JSON when the receiving environment cannot preserve large integers. Convert the string to ",[22,253,254],{},"BigInt"," where supported. A JSON number may already have lost its final digits before your converter receives it.",[138,257,258,259,263],{},"For SQL, check the database engine and session time-zone setting. Functions with similar names can return different types or apply a session zone. Store the unit contract in the schema or API documentation and verify a known fixture after every import. See ",[180,260,262],{"href":261},"/guides/iso-8601-and-unix-time/","ISO and Unix time"," for interchange format choices.",[10,265,267],{"id":266},"standards-and-runtime-references","Standards and runtime references",[269,270,271,280],"ul",{},[272,273,274],"li",{},[180,275,279],{"href":276,"rel":277},"https://docs.python.org/3/library/time.html#time.time_ns",[278],"nofollow","Python time.time_ns()",[272,281,282],{},[180,283,286],{"href":284,"rel":285},"https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date.now",[278],"ECMAScript Date.now()",[288,289,290],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":20,"searchDepth":53,"depth":53,"links":292},[293,294,295,296,297,298,299,300],{"id":12,"depth":53,"text":13},{"id":81,"depth":53,"text":82},{"id":107,"depth":53,"text":108},{"id":127,"depth":53,"text":128},{"id":143,"depth":53,"text":144},{"id":187,"depth":53,"text":188},{"id":243,"depth":53,"text":244},{"id":266,"depth":53,"text":267},"Practical examples for retrieving Unix seconds or milliseconds in JavaScript, Python, Java, PHP, Go, SQL and Shell.","md","Guide / code",null,"en",{},"guide","/en/guides/get-current-timestamp-in-code",{"title":310,"description":311},"Get Current Unix Timestamp in JavaScript, Python, Java & More","Copy clear examples for current Unix seconds and milliseconds in JavaScript, Python, Java, PHP, Go, SQL and Shell.","en/guides/get-current-timestamp-in-code","sxTRg0dtHd8XcCH6F_8eWWC_uavWqohJkbrWQfWEzYY",1792032483057]