22
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi muốn chuyển đổi dd/mm/yyyy sang mm/dd/yyyy trong javascript.
Roman
18,9K5 Huy hiệu vàng65 Huy hiệu bạc83 Huy hiệu Đồng5 gold badges65 silver badges83 bronze badges
Đã hỏi ngày 25 tháng 3 năm 2011 lúc 13:47Mar 25, 2011 at 13:47
6
var initial = 'dd/mm/yyyy'.split[/\//];
console.log[ [ initial[1], initial[0], initial[2] ].join['/']]; //=> 'mm/dd/yyyy'
Chỉnh sửa 2021/05/14: Một đoạn trích bằng ES20XX: A snippet using ES20xx
const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
Đã trả lời ngày 25 tháng 3 năm 2011 lúc 13:50Mar 25, 2011 at 13:50
KooiinckooiincKooiInc
Huy hiệu vàng 115K3131 gold badges141 silver badges176 bronze badges
1
var date = "24/09/1977";
var datearray = date.split["/"];
var newdate = datearray[1] + '/' + datearray[0] + '/' + datearray[2];
NewDate sẽ chứa 09/24/1977. Phương thức phân chia sẽ phân chia chuỗi bất cứ nơi nào nó tìm thấy "/", vì vậy nếu chuỗi ngày là "24/9/1977", nó vẫn sẽ hoạt động và trả lại ngày 24/9/1977.
Đã trả lời ngày 25 tháng 3 năm 2011 lúc 13:50Mar 25, 2011 at 13:50
KooiinckooiincSurreal Dreams
Huy hiệu vàng 115K313 gold badges45 silver badges61 bronze badges
1
NewDate sẽ chứa 09/24/1977. Phương thức phân chia sẽ phân chia chuỗi bất cứ nơi nào nó tìm thấy "/", vì vậy nếu chuỗi ngày là "24/9/1977", nó vẫn sẽ hoạt động và trả lại ngày 24/9/1977.
//specify the date string and the format it's initially in
var mydate = moment['15/11/2000', 'DD/MM/YYYY'];
//format that date into a different format
moment[mydate].format["MM/DD/YYYY"];
//outputs 11/15/2000
Những giấc mơ siêu thựcOct 6, 2016 at 1:56
will-yamawill-yamawill-yama
25.5K3 Huy hiệu vàng45 Huy hiệu bạc61 Huy hiệu Đồng5 silver badges10 bronze badges
Đây là một thời điểm.js phiên bản. Thư viện có thể được tìm thấy tại //momentjs.com/
months = {'jan': '01', 'feb': '02', 'mar': '03', 'apr': '04', 'may': '05',
'jun': '06', 'jul': '07', 'aug': '08', 'sep': '09', 'oct': '10', 'nov': '11',
'dec': '12'};
split = 'dd/mon/yyyy'.split['/'];
[months[split[1]], split[0], split[2]].join['/'];
Đã trả lời ngày 6 tháng 10 năm 2016 lúc 1:56
split = 'dd/mm/yyyy'.split['/'];
[split[1], split[0], split[2]].join['/'];
6625 Huy hiệu bạc10 Huy hiệu ĐồngMar 25, 2011 at 13:55
Chuyển đổi
const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
5 thành const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
6:Sylvain DefresneChuyển đổi
7 thành const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
6:11 gold badges73 silver badges83 bronze badgesconst pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
0
Đã trả lời ngày 25 tháng 3 năm 2011 lúc 13:55
Sylvain defresnesylvain defresne
41K11 Huy hiệu vàng73 Huy hiệu bạc83 Huy hiệu đồng37 gold badges322 silver badges275 bronze badges
var months = new Array["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];
var ddSplit = '12/23/2011'.split['/']; //date is mm/dd/yyyy format
alert[ddSplit[1]+'-'+months[ddSplit[0]-1] + '-' + ddSplit[2]]
Dec 24, 2011 at 15:20
Tim Cooper
const setFormatDDMMYYYYtoMMDDYYYY = [date, separator = '/'] => {
const [day, month, year] = date.split['/'];
return month + separator + day + separator + year;
};
154K37 Huy hiệu vàng322 Huy hiệu bạc275 Huy hiệu ĐồngDec 28, 2019 at 18:35
1
Đã trả lời ngày 24 tháng 12 năm 2011 lúc 15:20
var d1 = "03/25/2011";
var d2 = d1.replace[/^[\d{1,2}\/][\d{1,2}\/][\d{4}]$/,"$2$1$3"];
alert["d1:"+d1 +"\n d2:"+d2 ]
/*
// OUTPUT
d1: 03/25/2011
d2: 25/03/2011
*/
EDIT:
Bạn có thể sử dụng chức năng sau được chỉ định, 1 ngày và 2 Bộ phân cách ngày [tùy chọn] Ngày sẽ được trả về ở định dạng mong muốn.
Đã trả lời ngày 28 tháng 12 năm 2019 lúc 18:35Mar 25, 2011 at 14:17
Hãy thử ví dụ này trong đó một biểu thức thông thường được sử dụng để tìm và chuyển các phần của chuỗi:leoinfo
Tôi nhận thấy rằng không có câu trả lời nào khác đang sử dụng biểu thức chính quy và tôi thực sự tự hỏi tại sao ... :]8 gold badges35 silver badges48 bronze badges
1
Đã trả lời ngày 25 tháng 3 năm 2011 lúc 14:17
var fechaLatinFormat= "05-10-2016";//octuber 5, 2016 - octubre 5 del 2016
var datePieces = fechaLatinFormat.split["-"];
var preFinalDate = new Date[parseInt[datePieces[2]], parseInt[datePieces[1]] - 1, parseInt[datePieces[0]]];
console.log[preFinalDate.format["mm-dd-yyyy"]];
Or:
const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
0
LEOINFOLEOINFO
7.7108 Huy hiệu vàng35 Huy hiệu bạc48 Huy hiệu đồng146 gold badges85 silver badges118 bronze badges
Hãy thử điều này, nó hoạt động cho tôi:Oct 6, 2016 at 0:50
PangMk Sz
9.223146 Huy hiệu vàng85 Huy hiệu bạc118 Huy hiệu đồng4 bronze badges
const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
1Đã trả lời ngày 6 tháng 10 năm 2016 lúc 0:50
MK SZMK SZMar 9, 2017 at 13:53
Phù hiệu bằng đồng 214Johan Hoeksma
Đây chỉ là giải pháp cho những con số với regex này5 gold badges26 silver badges36 bronze badges
const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
2Đã trả lời ngày 9 tháng 3 năm 2017 lúc 13:53Mar 25, 2011 at 13:53
Johan Hoeksmajohan HoeksmaTrevor Arjeski
3.3455 Huy hiệu vàng26 Huy hiệu bạc36 Huy hiệu Đồng1 gold badge24 silver badges39 bronze badges
const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
3Đã trả lời ngày 25 tháng 3 năm 2011 lúc 13:53Mar 25, 2011 at 13:57
fl00rfl00rfl00r
Trevor Arjeskitrevor Arjeski32 gold badges215 silver badges235 bronze badges
2.0681 Huy hiệu vàng24 Huy hiệu bạc39 Huy hiệu đồng
const pad = v => v.padStart[2, `0`];
const initialDate= new Date[].toLocaleDateString["nl-NL"]
.split[/[-/]/].map[pad].join["/"];
const toFragments = dateString => initialDate
.split[/[-/]/].map[pad];
const dateTo_mmddyyyy = [[date, month, year], divider = "/"] =>
`${month}${divider}${date}${divider}${year}`;
const [date, month, year] = toFragments[initialDate];
console.log[ `initial [dd/mm/yyyy]: ${initialDate}`];
console.log[ `reformatted to mm/dd/yyyy [array join]: ${
[month, date, year].join['/'] }` ];
console.log[ `reformatted to mm-dd-yyyy [function]: ${
dateTo_mmddyyyy[toFragments[initialDate], "-"] }` ];
4Đã trả lời ngày 25 tháng 3 năm 2011 lúc 13:57
81.9K32 Huy hiệu vàng215 Huy hiệu bạc235 Huy hiệu Đồng
Sử dụng chức năng nàyApr 19, 2012 at 12:25
// Sử dụng cùng tên cho tháng và năm ở cả hai định dạngarun
Bạn có thể sử dụng chức năng này để chuyển đổi bất kỳ ngày định dạng nào [chỉ bao gồm cả năm tháng] sang bất kỳ định dạng nào. :]3 gold badges28 silver badges54 bronze badges