[英]how to convert date one formate to another in javascript?
could you please tell me how to convert date one formate to another in javascript ? 你能告诉我如何在javascript中将日期一个转换为另一个吗?
var date = new Date("24 May 2017, 05:35");
d=date.getDate();
m=date.getMonth();
y=date.getYear();
h=date.getHours();
m=date.getMinutes();
console.log(m +' '+d +' '+y+', ' + h+m)
// expected output
//May 24, 2017, 05.35 AM IST
https://jsfiddle.net/bbbnxfz8/ https://jsfiddle.net/bbbnxfz8/
I don't want to use any library like moment
我不想使用像任何库
moment
For example if you have 例如,如果你有
var date = new Date("24 May 2017, 05:35");
var day1 = date.getDate();
You can convert like this --> 你可以像这样转换 - >
var date2 = new Date(date);
var day = date2.getDay();
You can pass a object always content a date form 您可以传递始终满足日期表单的对象
Do you like this? 你喜欢吗?
// Creating Date object
var date = new Date("24 May 2017, 05:35");
// fetching date
d = date.getDate();
// fetching month
m = date.getMonth();
// fetching year
y = date.getFullYear();
// fetching hours
h = date.getHours();
// fetching minutes
min= date.getMinutes();
// creating month's name array to display name of month
var monthArr = new Array();
monthArr = ['Jan','Feb','March','April','May']; // you can add all months
// Code to show time as per requirement
var ampm = h >= 12 ? 'pm' : 'am';
h = h % 12;
hours = h ? h : 12; // the hour '0' should be '12'
minutes = min < 10 ? '0'+min : min;
var strTime = hours + '.' + minutes + ' ' + ampm;
var finalDate = monthArr[m]+' '+d+','+y+', '+strTime;
console.log(finalDate);
I hope this helps you. 我希望这可以帮助你。
As you stated the problem, I had a solution, tweak it as you want. 正如你所说的那样,我有一个解决方案,可以根据需要进行调整。 Also try this Fiddle DEMO
也试试这个小提琴演示
function dateFormater(dateStr, format, separator) {
var date = new Date(dateStr),
formatArr = format.split('-'),
len = formatArr.length,
str = '',
i,
monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
],
getdata = function(d) {
function getCurrentTime(d) {
var currentTime,
hour = d.getHours(),
meridiem = hour >= 12 ? "PM" : "AM";
return ((hour + 11) % 12 + 1) + ":" + d.getMinutes() + ' ' + meridiem;
}
switch (d) {
case 'day':
return getDay(date.getDay);
case 'month':
return monthNames[date.getMonth()];
case 'dd':
return date.getDate();
case 'mm':
return date.getMonth();
case 'yy':
return date.getFullYear();
case 'time':
return getCurrentTime(date);
case 'zone':
return date.toString().match(/\(([A-Za-z\s].*)\)/)[1];
}
},
getDay = function(d) {
switch (d) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}
};
for (i = 0; i < len; i++) {
i === len - 1 && (separator = '');
str += getdata(formatArr[i]) + separator;
}
return str;
}
var format = 'month-dd-yy-time-zone';
console.log(dateFormater("24 May 2017, 05:35", format, ', ')); // May, 24, 2017, 5:35 AM, IST
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.