我对Javascript了解不多,我发现的其他问题与日期操作有关,不仅仅是根据需要获取信息.
我希望得到以下格式的日期:
于2011年1月27日星期四17:42:21印刷
到目前为止,我得到了以下内容:
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
h = checkTime(h);
m = checkTime(m);
s = checkTime(s);
var prnDt = "Printed on Thursday, " + now.getDate() + " January " + now.getFullYear() + " at " + h + ":" + m + ":" s;
Run Code Online (Sandbox Code Playgroud)
我现在需要知道如何获得星期几和一年中的某个月(他们的名字).
有没有一种简单的方法来制作它,或者我是否应该考虑使用数组,我只需使用now.getMonth()和索引到正确的值now.getDay()?
如何以整数格式获取一周中的哪一天?我知道ToString只返回一个字符串.
DateTime ClockInfoFromSystem = DateTime.Now;
int day1;
string day2;
day1= ClockInfoFromSystem.DayOfWeek.ToString(); /// it is not working
day2= ClockInfoFromSystem.DayOfWeek.ToString(); /// it gives me string
Run Code Online (Sandbox Code Playgroud) Oracle的表服务器提供内置函数TRUNC(timestamp,'DY').此函数将上一个星期日的任何时间戳转换为午夜.在MySQL中执行此操作的最佳方法是什么?
Oracle还提供TRUNC(timestamp,'MM')将时间戳转换为发生月份的第一天的午夜.在MySQL中,这个很简单:
TIMESTAMP(DATE_FORMAT(timestamp, '%Y-%m-01'))
Run Code Online (Sandbox Code Playgroud)
但这个DATE_FORMAT技巧几周不会奏效.我知道这个WEEK(timestamp)功能,但我真的不想要一年内的周数; 这个东西是多年的工作.
鉴于03/09/1982我们怎么能说这是哪个工作日.在这种情况下它将是Tue.
是否可以获得单个查询?
我使用Sakamoto的算法来查找给定日期的星期几.谁能告诉我这个算法的正确性?我想从2000年到2099年.
维基百科的算法仅供参考.
int dow(int y, int m, int d)
{
static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
y -= m < 3;
return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
Run Code Online (Sandbox Code Playgroud) 我试着通过这种方式得到一个带有实际工作日名称的字符串:
Calendar c = Calendar.getInstance();
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if (c.get(Calendar.MONDAY) == dayOfWeek) weekDay = "monday";
else if (c.get(Calendar.TUESDAY) == dayOfWeek) weekDay = "tuesday";
else if (c.get(Calendar.WEDNESDAY) == dayOfWeek) weekDay = "wednesday";
else if (c.get(Calendar.THURSDAY) == dayOfWeek) weekDay = "thursday";
else if (c.get(Calendar.FRIDAY) == dayOfWeek) weekDay = "friday";
else if (c.get(Calendar.SATURDAY) == dayOfWeek) weekDay = "saturday";
else if (c.get(Calendar.SUNDAY) == dayOfWeek) weekDay = "sunday";
Run Code Online (Sandbox Code Playgroud)
但weekDay保持总是为空,我实际上不知道为什么因为调试器说dayOfWeek是5所以我应该等于c.get(Calendar.THURSDAY)
除了今天之外,我如何本地化DayOfWeek?
以下工作在当天适用:
DateTime.Now.ToString("dddd", new CultureInfo("it-IT"));
Run Code Online (Sandbox Code Playgroud)
但是我如何本地化一周的所有日子?
编辑:我想出的一个可能(也可能是非常非常错误的)解决方案可能是创建DateTime一整周的值,然后使用date.ToString("dddd", new CultureInfo("it-IT"));它们,但这在很多层面上似乎都是错误的.
最简单的方法:
String[] namesOfDays = new String[7] {
"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"
};
Run Code Online (Sandbox Code Playgroud)
此方法不使用Locale.因此,如果系统的语言不是英语,则此方法无法正常工作.
使用Joda时间,我们可以这样做:
String[] namesOfDays = new String[7];
LocalDate now = new LocalDate();
for (int i=0; i<7; i++) {
/* DateTimeConstants.MONDAY = 1, TUESDAY = 2, ..., SUNDAY = 7 */
namesOfDays[i] = now.withDayOfWeek((DateTimeConstants.SUNDAY + i - 1) % 7 + 1)
.dayOfWeek().getAsShortText();
}
Run Code Online (Sandbox Code Playgroud)
但是,此方法使用今天的日期和日历计算,这对于最终目的而言是无用的.而且,它有点复杂.
是否有一个简单的方法来得到这样的字符串"Sun","Mon",...,"Sat"用系统的默认语言环境?
我们有一个实用工具,将在周一至周五之间的任何一天运行.它将更新内容管理工具中的一些文件.与该文件关联的最后修改日期应该是该周的星期一日期.我编写了以下程序来检索当前周的星期一日期.但我仍然不确定这是否适用于所有情况.有没有人有更好的解决方案?
Calendar c = Calendar.getInstance();
c.setTime(new Date());
System.out.println(c.get(Calendar.DAY_OF_MONTH));
System.out.println(c.get(Calendar.DAY_OF_WEEK));
int mondayNo = c.get(Calendar.DAY_OF_MONTH)-c.get(Calendar.DAY_OF_WEEK)+2;
c.set(Calendar.DAY_OF_MONTH,mondayNo);
System.out.println("Date "+c.getTime());
Run Code Online (Sandbox Code Playgroud) 如标题中所述,我有一个周数和一年的值.有没有办法使用moment.js找到那周的星期一?正在尝试,但没有成功