嗨我想在javascript:2011年4月12日06:42:03将当前日期转换为如下格式.
有什么建议?????
我正在从网址抓取JSON数据.时间是军事时间,我想知道是否有一种方法,一旦我在客户端检索将其转换为标准时间.
这是JSON:
[
{
SaturdayClose: "21:00",
SaturdayOpen: "10:00",
SundayClose: "12:00",
SundayOpen: "18:00",
WeekdayClose: "21:00",
WeekdayOpen: "10:00"
}
]
Run Code Online (Sandbox Code Playgroud)
这是我的index.html:
<p>
Sun: ${ SundayOpen }a - ${ SundayClose }p Mon - Sat: ${ SaturdayOpen }a ${ SaturdayClose }p
</p>
Run Code Online (Sandbox Code Playgroud)
这会返回这种丑陋:
太阳:18:00a - 12:00p周一至周六:10:00a 21:00p
我宁愿回复这个:
太阳:6:00a - 12:周一至周六:10:00a - 9:00p
我从服务器收到一个字符串形式的日期时间,可能如下所示:
07/08/2012 13:17:32
Run Code Online (Sandbox Code Playgroud)
这是 UTC 时间。
或者它可能具有以下格式的时区:
07/08/2012 13:17:32 UTC+01:00
Run Code Online (Sandbox Code Playgroud)
我需要一种通用方法将其解析为Date要显示的对象。如果我这样做,var d = new Date(str)那么它假设的第一个例子是当地时间。
编辑:字符串中可能并不总是“UTC”,我认为它可能是 GMT、Z 或任何其他时区指示符。
有任何想法吗?
可能的重复:
在 JavaScript 中格式化日期
我需要以这样的格式显示当前日期(一些示例):
sep 10, 2012
nov 5, 2012
Run Code Online (Sandbox Code Playgroud)
等等。
使用javascript我获取当前日期对象
var date = new Date();
Run Code Online (Sandbox Code Playgroud)
我接下来需要做什么?
我已经检查了此SO帖子:在哪里可以找到有关在JavaScript中格式化日期的文档?
我也看过http://home.clara.net/shotover/datetest.htm
我的字符串是: Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)
我想将其转换为dd-mm-yyyy格式。
我尝试使用:
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();
Run Code Online (Sandbox Code Playgroud)
但这给我的结果是: 1-6-2013
该getDay()值是一周中一天的索引。
例如,如果my dateString是Thu Jun 20 2013 05:30:00 GMT+0530 (India Standard Time)
,则输出为4-6-2013
如何获得Day的适当价值?
PS:我尝试使用它.toLocaleString()并从中创建新的日期对象。但这给出了相同的结果。
这可能是一个简单的问题,但我遇到了这个日期选择器的问题.问题是我将格式设置为dd/mm/yyyywith data-date-format属性.但是,检查我ng-model的值时如下:Wed Jul 17 2013 00:00:00 GMT+0000 (Greenwich Standard Time)
我想要的是绑定到dd/mm/yyyy格式.
我怎样才能解决这个问题?
这是我的代码:
<label for="inputDateFrom">Frá</label>
<div class="control-group input-append">
<input type="text" ng-model="booking.Booking.DateFrom" data-date-format="dd/mm/yyyy" bs-datepicker>
<button type="button" class="btn" data-toggle="datepicker"><i class="icon-calendar"></i></button>
</div>
Run Code Online (Sandbox Code Playgroud)
更新18.07.13:
根据rGil的回答,我尝试使用$ scope.$ watch.它工作正常,但首先它获得CORRECT日期(来自getBooking()服务函数),然后它更改为CURRENT日期 - 这不是日期.
JavaScript代码如下:
$scope.$watch('booking.Booking.DateFrom', function(v){ // using the example model from the datepicker docs
$scope.booking.Booking.DateFrom = moment(v).format();
alert(moment(v).format());
});
$scope.$watch('booking.Booking.DateTo', function(v){ // using the example model from the datepicker docs
$scope.booking.Booking.DateTo = moment(v).format();
alert(moment(v).format());
});
// Sækjum …Run Code Online (Sandbox Code Playgroud) 我有一个以 HTML 显示的日期。日期当前显示为2012-03-12。所以现在,我想将此日期显示为单词,即它应该显示为12 March 2012. 下面是我使用的 HTML 代码。
<tr>
<th>Date of Birth: </th>
<td>{{dob}}</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
此处,dob包含必须转换为单词的值。我怎样才能做到这一点?
我使用以下代码片段将 Date 对象转换为 string 。
var startDate = new Date();
var result = Globalize.parseDate(startDate, "MM/DD/YYYY");
Run Code Online (Sandbox Code Playgroud)
但它会返回空值。如何将日期对象转换为字符串特定格式?
我的时间格式如下:12/16/2011 3:49:37 PM我得到的格式是:
var newDate = new Date(timeFromat);
timeFormat = newDate.toLocaleString();
Run Code Online (Sandbox Code Playgroud)
我的实际格式是格林尼治标准时间,我使用上面的代码将其转换为我的预订时间.
我想将它改为24小时格式,所以我希望这个日期改变如:12/16/2011 15:49:37我想在javascript中做到这一点.
多数民众赞成我所做的
var firstPartOftimeFormat = timeFormat.substring(0,9);
var secondPartOftimeFormat = timeFormat.substring(10,20);
Run Code Online (Sandbox Code Playgroud)
但是当日期格式如下时它不起作用:3/16/2011.但以下部分有效.
var time = $("#starttime").val();
var hours = Number(secondPartOftimeFormat.match(/^(\d+)/)[1]);
var minutes = Number(secondPartOftimeFormat.match(/:(\d+)/)[1]);
var AMPM = secondPartOftimeFormat.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + …Run Code Online (Sandbox Code Playgroud) 我无法为表单中的“datetime-local”元素赋值。
模板中的代码:
打字稿文件中的代码:
let dateTime : Date = new Date();
this.testForm = this.formBuilder.group({
'dateTime': new FormControl(dateTime),
});
Run Code Online (Sandbox Code Playgroud)
结果:
使用打字稿将日期和时间分配给 datetime-local 的正确方法是什么
javascript ×8
date ×2
datetime ×2
html ×2
angular ×1
angular5 ×1
angularjs ×1
date-format ×1
datepicker ×1
format ×1
jquery ×1
json ×1