最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get exact day from date string in Javascript - Stack Overflow

programmeradmin4浏览0评论

I have checked this SO post: Where can I find documentation on formatting a date in JavaScript?

Also I have looked into .htm

My string is: Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)

And I want to convert it to dd-mm-yyyy format.

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

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

How can I get the proper value of Day?

P.S: I tried using .toLocaleString() and creating new date object from it. But it gives the same result.

I have checked this SO post: Where can I find documentation on formatting a date in JavaScript?

Also I have looked into http://home.clara.net/shotover/datetest.htm

My string is: Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)

And I want to convert it to dd-mm-yyyy format.

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

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

How can I get the proper value of Day?

P.S: I tried using .toLocaleString() and creating new date object from it. But it gives the same result.

Share Improve this question edited May 23, 2017 at 11:53 CommunityBot 11 silver badge asked Jun 11, 2013 at 6:52 Prasad JadhavPrasad Jadhav 5,20816 gold badges64 silver badges80 bronze badges 0
Add a comment  | 

5 Answers 5

Reset to default 7

To get the day of the month use getDate():

var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();

W3 schools suggests just building your days of the week array and using it:

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.

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().

The above will return the local date for each date part, use the UTC variants if you need the universal time.

I think you will have to take an Array of the days & utilize it using the received index from the getDay() method.

To get required format with given date will achieve with moment.js. a one liner solution is

import moment from "moment";

const date = new Date();
const finalDate = moment(date).format("DD-MM-YYYY")
发布评论

评论列表(0)

  1. 暂无评论