English 中文(简体)
如何将字符串转换为 Javascript 中的日期对象? [重复]
原标题:How to convert string to date object in javascript? [duplicate]
  • 时间:2019-06-19 08:46:17
  •  标签:
  • javascript
This question already has answers here: Parsing a string to a date in JavaScript (35 answers) Closed 5 years ago. I have a date string like 2019-06-12 12:02:12 232 ,how to convert to date using Javascript? Date date = Date.parse( 2019-06-12 12:02:12 232 ).format( yyyy-MM-dd dd:HH:ss SSS ); I want to compare two date to calculate the minisecond in Javascript.Any suggestion?How could I tweak my date string?The date string was generate is server side using Java: DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS"); String dateTime = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter); This question does not contain minisecond,if no minisecond,Date.parse could solved the problem.My question is precise contain more detail operation of Javascript Date.
最佳回答
You can do it like that: const dateString = "2019-06-12 12:02:12 232" const [y,M,d,h,m,s] = dateString.split(/[- :]/); const yourDate = new Date(y,parseInt(M)-1,d,h,parseInt(m),s); console.log(`yourDate ${yourDate}`); In addition, avoid using Date.parse() method. UPDATE: By using the above approach, you will avoid some strange bugs as described in the above link. However, it is possible to use a great library Moment.js: moment("12-25-1995", "MM-DD-YYYY"); Moreover, you could check if a date is valid: moment("whether this date is valid").isValid(); // OUTPUT: false
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签