javascript 从Javascript中的日期字符串中获取确切的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17038105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get exact day from date string in Javascript
提问by Prasad Jadhav
I have checked this SO post: Where can I find documentation on formatting a date in JavaScript?
我已经检查了这篇 SO 帖子:我在哪里可以找到有关在 JavaScript 中格式化日期的文档?
Also I have looked into http://home.clara.net/shotover/datetest.htm
我也看过http://home.clara.net/shotover/datetest.htm
My string is: Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)
我的字符串是: Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)
And I want to convert it to dd-mm-yyyy
format.
我想将其转换为dd-mm-yyyy
格式。
I tried using:
我尝试使用:
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDay()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
But it gives me the result as: 1-6-2013
但它给了我这样的结果: 1-6-2013
The getDay()
value is the index of day in a week.
For Instance,
If my dateString
is Thu Jun 20 2013 05:30:00 GMT+0530 (India Standard Time)
it gives output as 4-6-2013
该getDay()
值是一周中一天的索引。
例如,如果我dateString
是Thu Jun 20 2013 05:30:00 GMT+0530 (India Standard Time)
它,则输出为4-6-2013
How can I get the proper value of Day?
如何获得 Day 的正确值?
P.S: I tried using .toLocaleString()
and creating new date object from it. But it gives the same result.
PS:我尝试使用它.toLocaleString()
并从中创建新的日期对象。但它给出了相同的结果。
采纳答案by Sirko
回答by mike
W3 schools suggests just building your days of the week array and using it:
W3 学校建议只构建您的星期几数组并使用它:
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var n = weekday[d.getDay()];
Not super elegant, but usable.
不是超级优雅,但可用。
回答by Sparko
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
Replace getDay() with getDate().
用 getDate() 替换 getDay()。
The above will return the local date for each date part, use the UTC variants if you need the universal time.
以上将返回每个日期部分的本地日期,如果您需要通用时间,请使用 UTC 变体。
回答by powercoder23
I think you will have to take an Array of the days & utilize it using the received index from the getDay()
method.
我认为您将不得不获取天数的数组并使用从该getDay()
方法接收到的索引来利用它。