jQuery 从字符串更改数据格式

发布于 2024-09-24 15:02:47 字数 153 浏览 5 评论 0原文

我有这个字符串: 2010-09-21T15:48:12.754+02:00

我会将其转换为以下格式: Tue Sep 21 15:48:12 CEST 2010

我如何使用 javascript 或 jQuery 做到这一点?

多谢。 议员。

I have this string:
2010-09-21T15:48:12.754+02:00

I would transform it in this format:
Tue Sep 21 15:48:12 CEST 2010

How can I do it with javascript or jQuery?

Thanks a lot.
MP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

離殇 2024-10-01 15:02:47

试试这个:

var myDate = new Date("2010-09-21T15:48:12.754+02:00")
alert(myDate);

编辑

如果你愿意,你可以编写自己的日期格式,例如:

var date = new Date("2010-09-21T15:48:12.754+02:00");
var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; 
var myDate = '';
myDate += days[date.getDay()] + " " + months[date.getMonth()] + " " + date.getDate() + " ";
myDate += date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + " ";
myDate += "0" + date.getTimezoneOffset() / 60 + ":00 " + date.getFullYear();
alert(myDate);

参见jsfiddle


编辑

另一个例子:

// crazy date
var date = new Date("2010-09-22");
var days = ['Weekend!', 'Workday', 'Workday', 'Workday', 'Workday', 'Workday', 'Weekend!'];
var months = ['Winter', 'Winter', 'Spring', 'Spring', 'Spring', 'Summer', 'Summer', 'Summer', 'Autumn', 'Autumn', 'Autumn', 'Winter'];
var myDate = "We are in " + months[date.getMonth()] + " and today is a " +  days[date.getDay()];
alert(myDate);

Try this:

var myDate = new Date("2010-09-21T15:48:12.754+02:00")
alert(myDate);

EDIT

If you want, you can write your own date format, for example:

var date = new Date("2010-09-21T15:48:12.754+02:00");
var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; 
var myDate = '';
myDate += days[date.getDay()] + " " + months[date.getMonth()] + " " + date.getDate() + " ";
myDate += date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + " ";
myDate += "0" + date.getTimezoneOffset() / 60 + ":00 " + date.getFullYear();
alert(myDate);

See in jsfiddle.


EDIT

Another example:

// crazy date
var date = new Date("2010-09-22");
var days = ['Weekend!', 'Workday', 'Workday', 'Workday', 'Workday', 'Workday', 'Weekend!'];
var months = ['Winter', 'Winter', 'Spring', 'Spring', 'Spring', 'Summer', 'Summer', 'Summer', 'Autumn', 'Autumn', 'Autumn', 'Winter'];
var myDate = "We are in " + months[date.getMonth()] + " and today is a " +  days[date.getDay()];
alert(myDate);
暖心男生 2024-10-01 15:02:47

试试这个:

var d = new Date("2010-09-21T15:48:12.754+02:00");
var date = d.toDateString().split(/\s/);
var time = d.getHours() +':'+ d.getMinutes() +':'+ d.getSeconds();
date.splice(3, 3, time, 'CEST', d.getFullYear());
date.join(' '); // Tue Sep 21 15:48:12 CEST 2010 

Try this:

var d = new Date("2010-09-21T15:48:12.754+02:00");
var date = d.toDateString().split(/\s/);
var time = d.getHours() +':'+ d.getMinutes() +':'+ d.getSeconds();
date.splice(3, 3, time, 'CEST', d.getFullYear());
date.join(' '); // Tue Sep 21 15:48:12 CEST 2010 
别再吹冷风 2024-10-01 15:02:47

根据 Topera,创建一个日期对象,然后使用对象方法创建您喜欢的格式的字符串。

这是一个参考:http://www.w3schools.com/jsref/jsref_obj_date.asp

As per Topera, create a date object, then use the objects methods to create the string formatted as you like.

Here's a reference: http://www.w3schools.com/jsref/jsref_obj_date.asp

剩余の解释 2024-10-01 15:02:47

解决了。

var datasplit=obj.requests[i].start.split("T");

                    datasplit[1]=datasplit[1].slice(0,7);

                    var d = new Date(datasplit[0]+" "+datasplit[1]);
                    var date = d.toDateString().split(/\s/);
                    var time = d.getHours() +':'+ d.getMinutes() +':'+ d.getSeconds();
                    date.splice(3, 3, time, 'CEST', d.getFullYear());
                    date.join(' '); // Tue Sep 21 15:48:12 CEST 2010
                    //alert(date);
                    var data=date.toString();
                    data=data.replace(/,/g," ");

谢谢。

Solved.

var datasplit=obj.requests[i].start.split("T");

                    datasplit[1]=datasplit[1].slice(0,7);

                    var d = new Date(datasplit[0]+" "+datasplit[1]);
                    var date = d.toDateString().split(/\s/);
                    var time = d.getHours() +':'+ d.getMinutes() +':'+ d.getSeconds();
                    date.splice(3, 3, time, 'CEST', d.getFullYear());
                    date.join(' '); // Tue Sep 21 15:48:12 CEST 2010
                    //alert(date);
                    var data=date.toString();
                    data=data.replace(/,/g," ");

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文